Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit e301f8a

Browse files
authored
Merge pull request #978 from storyblok/bugfix/941-inconsistent-promise-resolutionrejection-in-case-of-a-network-error
fix: 941 inconsistent promise resolutionrejection in case of a network error
2 parents 3456442 + 3acfae8 commit e301f8a

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"rules": {
9797
"body-max-line-length": [
9898
2,
99-
"always",
99+
"never",
100100
200
101101
],
102102
"footer-max-line-length": [

src/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ class Storyblok {
268268
): Promise<ISbResponseData> {
269269
const url = `/${slug}`;
270270

271-
return Promise.resolve(
272-
this.throttle('post', url, params, fetchOptions),
273-
) as Promise<ISbResponseData>;
271+
return this.throttle('post', url, params, fetchOptions) as Promise<ISbResponseData>;
274272
}
275273

276274
public put(
@@ -280,9 +278,7 @@ class Storyblok {
280278
): Promise<ISbResponseData> {
281279
const url = `/${slug}`;
282280

283-
return Promise.resolve(
284-
this.throttle('put', url, params, fetchOptions),
285-
) as Promise<ISbResponseData>;
281+
return this.throttle('put', url, params, fetchOptions) as Promise<ISbResponseData>;
286282
}
287283

288284
public delete(
@@ -295,9 +291,7 @@ class Storyblok {
295291
}
296292
const url = `/${slug}`;
297293

298-
return Promise.resolve(
299-
this.throttle('delete', url, params, fetchOptions),
300-
) as Promise<ISbResponseData>;
294+
return this.throttle('delete', url, params, fetchOptions) as Promise<ISbResponseData>;
301295
}
302296

303297
public getStories(

src/sbFetch.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,44 @@ class SbFetch {
168168
this.ejectInterceptor = true;
169169
}
170170

171+
/**
172+
* Normalizes error messages from different response structures
173+
* @param data The response data that might contain error information
174+
* @returns A normalized error message string
175+
*/
176+
private _normalizeErrorMessage(data: any): string {
177+
// Handle array of error messages
178+
if (Array.isArray(data)) {
179+
return data[0] || 'Unknown error';
180+
}
181+
182+
// Handle object with error property
183+
if (data && typeof data === 'object') {
184+
// Check for common error message patterns
185+
if (data.error) {
186+
return data.error;
187+
}
188+
189+
// Handle nested error objects (like { name: ['has already been taken'] })
190+
for (const key in data) {
191+
if (Array.isArray(data[key])) {
192+
return `${key}: ${data[key][0]}`;
193+
}
194+
if (typeof data[key] === 'string') {
195+
return `${key}: ${data[key]}`;
196+
}
197+
}
198+
199+
// If we have a slug, it might be an error message
200+
if (data.slug) {
201+
return data.slug;
202+
}
203+
}
204+
205+
// Fallback for unknown error structures
206+
return 'Unknown error';
207+
}
208+
171209
private _statusHandler(res: ISbResponse): Promise<ISbResponse | ISbError> {
172210
const statusOk = /20[0-6]/g;
173211

@@ -177,11 +215,9 @@ class SbFetch {
177215
}
178216

179217
const error: ISbError = {
180-
message: res.statusText,
218+
message: this._normalizeErrorMessage(res.data),
181219
status: res.status,
182-
response: Array.isArray(res.data)
183-
? res.data[0]
184-
: res.data.error || res.data.slug,
220+
response: res,
185221
};
186222

187223
reject(error);

0 commit comments

Comments
 (0)