Skip to content

Commit

Permalink
Updating AxiosError to include stack trace (#4624)
Browse files Browse the repository at this point in the history
Related to discussions here #2387

Attempt to capture the stack trace at the time the error is created in
order to maintain context about where the error originates from.

Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
despreston and jasonsaayman committed May 3, 2022
1 parent 8213da4 commit 7920d11
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/core/AxiosError.js
Expand Up @@ -14,6 +14,11 @@ var utils = require('../utils');
*/
function AxiosError(message, code, config, request, response) {
Error.call(this);

if (Error.captureStackTrace) {
Error.captureStackTrace(this);
}

this.message = message;
this.name = 'AxiosError';
code && (this.code = code);
Expand Down
3 changes: 2 additions & 1 deletion test/specs/core/AxiosError.spec.js
@@ -1,7 +1,7 @@
var AxiosError = require('../../../lib/core/AxiosError');

describe('core::AxiosError', function() {
it('should create an Error with message, config, code, request, response and isAxiosError', function() {
it('should create an Error with message, config, code, request, response, stack and isAxiosError', function() {
var request = { path: '/foo' };
var response = { status: 200, data: { foo: 'bar' } };
var error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
Expand All @@ -12,6 +12,7 @@ describe('core::AxiosError', function() {
expect(error.request).toBe(request);
expect(error.response).toBe(response);
expect(error.isAxiosError).toBe(true);
expect(error.stack).toBeDefined();
});
it('should create an Error that can be serialized to JSON', function() {
// Attempting to serialize request and response results in
Expand Down

0 comments on commit 7920d11

Please sign in to comment.