Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/browser/services/fetch-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export default class FetchClient extends Client {
})
.catch(error => {
if (!error.json) {
return Promise.reject(createError(this._formatResponse({ error, status: -1 })));
return Promise.reject(createError(this._formatResponse({ error, status: -1 }), error));
}

return error.text()
.then(this._parseText)
.then(body => Promise.reject(createError(this._formatResponse(error, body))));
.then(body => Promise.reject(createError(this._formatResponse(error, body), error)));
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/utils/error-factory.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import errors from '../errors';

export function createError(response) {
export function createError(error, response) {
for (const SDKError of errors) {
if (SDKError.hasError && SDKError.hasError(response)) {
return new SDKError(response);
if (SDKError.hasError && SDKError.hasError(error)) {
return new SDKError({ ...error, response });
}
}
}
2 changes: 1 addition & 1 deletion src/node/services/request-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class RequestClient extends Client {
url
})
.then(response => this._formatResponse(response))
.catch(response => Promise.reject(response instanceof RequestError ? response : createError(this._formatResponse(response.response))));
.catch(response => Promise.reject(response instanceof RequestError ? response : createError(this._formatResponse(response.response), response)));
}

_formatResponse({ body, headers, statusCode }) {
Expand Down
7 changes: 7 additions & 0 deletions test/core/utils/error-factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ describe('ErrorFactory', () => {
it('should create a `ValidationFailedError`', () => {
expect(createError({ body: { code: 'validation_failed' } })).toBeInstanceOf(ValidationFailedError);
});

it('should create add an `response` attribute when provided', () => {
const error = createError({ status: 403 }, { foo: 'bar' });

expect(error).toBeInstanceOf(ForbiddenError);
expect(error.response).toEqual({ foo: 'bar' });
});
});
});