mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Added toJSON to decorated Axios errors to faciliate serialization (#1625)
This commit is contained in:
committed by
Emily Morehouse
parent
b4c5d35d28
commit
6b44e80ade
@@ -17,5 +17,23 @@ module.exports = function enhanceError(error, config, code, request, response) {
|
|||||||
}
|
}
|
||||||
error.request = request;
|
error.request = request;
|
||||||
error.response = response;
|
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;
|
return error;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,4 +12,17 @@ describe('core::createError', function() {
|
|||||||
expect(error.request).toBe(request);
|
expect(error.request).toBe(request);
|
||||||
expect(error.response).toBe(response);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user