Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added toJSON to enhanced Axios errors to facilitate serialization #1625

Merged
merged 1 commit into from Aug 7, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/core/enhanceError.js
Expand Up @@ -17,5 +17,23 @@ module.exports = function enhanceError(error, config, code, request, response) {
}
error.request = request;
error.response = response;
error.toJSON = function() {
return {
// Standard
message: this.message,
name: this.name,
// Microsoft
description: this.description,
number: this.number,
// Mozilla
fileName: this.fileName,
lineNumber: this.lineNumber,
columnNumber: this.columnNumber,
stack: this.stack,
// Axios
config: this.config,
code: this.code
};
};
return error;
};
13 changes: 13 additions & 0 deletions test/specs/core/createError.spec.js
Expand Up @@ -12,4 +12,17 @@ describe('core::createError', function() {
expect(error.request).toBe(request);
expect(error.response).toBe(response);
});
it('should create an Error that can be serialized to JSON', function() {
// Attempting to serialize request and response results in
// TypeError: Converting circular structure to JSON
var request = { path: '/foo' };
var response = { status: 200, data: { foo: 'bar' } };
var error = createError('Boom!', { foo: 'bar' }, 'ESOMETHING', request, response);
var json = error.toJSON();
expect(json.message).toBe('Boom!');
expect(json.config).toEqual({ foo: 'bar' });
expect(json.code).toBe('ESOMETHING');
expect(json.request).toBe(undefined);
expect(json.response).toBe(undefined);
});
});