From dff283b47b653cbfac7f32aa1ceac7e2c14b3b7c Mon Sep 17 00:00:00 2001 From: Des Preston Date: Tue, 3 May 2022 12:46:10 -0400 Subject: [PATCH] Updating AxiosError to include stack trace (#4624) Related to discussions here https://github.com/axios/axios/issues/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 --- lib/core/AxiosError.js | 5 +++++ test/specs/core/AxiosError.spec.js | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/core/AxiosError.js b/lib/core/AxiosError.js index 52c806a..2125a4e 100644 --- a/lib/core/AxiosError.js +++ b/lib/core/AxiosError.js @@ -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); diff --git a/test/specs/core/AxiosError.spec.js b/test/specs/core/AxiosError.spec.js index 49a9a6f..b6023f2 100644 --- a/test/specs/core/AxiosError.spec.js +++ b/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); @@ -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