From d51accbea1faef6e3b74c7dfa636704a2332bfbb Mon Sep 17 00:00:00 2001 From: Denis Frenademetz Date: Wed, 18 Feb 2026 18:33:28 +0100 Subject: [PATCH] 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 --- lib/core/AxiosError.js | 6 ++++++ test/specs/core/AxiosError.spec.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/core/AxiosError.js b/lib/core/AxiosError.js index 14737173..f28b8861 100644 --- a/lib/core/AxiosError.js +++ b/lib/core/AxiosError.js @@ -7,6 +7,12 @@ class AxiosError extends Error { const axiosError = new AxiosError(error.message, code || error.code, config, request, response); axiosError.cause = error; axiosError.name = error.name; + + // Preserve status from the original error if not already set from response + if (error.status != null && axiosError.status == null) { + axiosError.status = error.status; + } + customProps && Object.assign(axiosError, customProps); return axiosError; } diff --git a/test/specs/core/AxiosError.spec.js b/test/specs/core/AxiosError.spec.js index 17495ad4..e5a8bc8c 100644 --- a/test/specs/core/AxiosError.spec.js +++ b/test/specs/core/AxiosError.spec.js @@ -49,6 +49,23 @@ describe('core::AxiosError', function () { AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }) instanceof AxiosError ).toBeTruthy(); }); + + it('should preserve status property from original error when response is not provided', function () { + const error = new Error('Network Error'); + error.status = 404; + + const axiosError = AxiosError.from(error, 'ERR_NETWORK', { foo: 'bar' }); + expect(axiosError.status).toBe(404); + }); + + it('should use response.status over error.status when response is provided', function () { + const error = new Error('Error'); + error.status = 500; + const response = { status: 404 }; + + const axiosError = AxiosError.from(error, 'ERR_BAD_REQUEST', {}, null, response); + expect(axiosError.status).toBe(404); + }); }); it('should be a native error as checked by the NodeJS `isNativeError` function', function () {