Skip to content

Commit d51accb

Browse files
fix(core): copy status from source error in AxiosError.from (#7403)
When creating an AxiosError from an existing error without a response object, the status property from the source error is now preserved. This ensures error status information is not lost during error conversion. Fixes #7364 Co-authored-by: Jay <[email protected]>
1 parent 3e30bbf commit d51accb

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/core/AxiosError.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ class AxiosError extends Error {
77
const axiosError = new AxiosError(error.message, code || error.code, config, request, response);
88
axiosError.cause = error;
99
axiosError.name = error.name;
10+
11+
// Preserve status from the original error if not already set from response
12+
if (error.status != null && axiosError.status == null) {
13+
axiosError.status = error.status;
14+
}
15+
1016
customProps && Object.assign(axiosError, customProps);
1117
return axiosError;
1218
}

test/specs/core/AxiosError.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ describe('core::AxiosError', function () {
4949
AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }) instanceof AxiosError
5050
).toBeTruthy();
5151
});
52+
53+
it('should preserve status property from original error when response is not provided', function () {
54+
const error = new Error('Network Error');
55+
error.status = 404;
56+
57+
const axiosError = AxiosError.from(error, 'ERR_NETWORK', { foo: 'bar' });
58+
expect(axiosError.status).toBe(404);
59+
});
60+
61+
it('should use response.status over error.status when response is provided', function () {
62+
const error = new Error('Error');
63+
error.status = 500;
64+
const response = { status: 404 };
65+
66+
const axiosError = AxiosError.from(error, 'ERR_BAD_REQUEST', {}, null, response);
67+
expect(axiosError.status).toBe(404);
68+
});
5269
});
5370

5471
it('should be a native error as checked by the NodeJS `isNativeError` function', function () {

0 commit comments

Comments
 (0)