mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
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 <jasonsaayman@gmail.com>
This commit is contained in:
committed by
GitHub
parent
3e30bbf1b3
commit
d51accbea1
@@ -7,6 +7,12 @@ class AxiosError extends Error {
|
|||||||
const axiosError = new AxiosError(error.message, code || error.code, config, request, response);
|
const axiosError = new AxiosError(error.message, code || error.code, config, request, response);
|
||||||
axiosError.cause = error;
|
axiosError.cause = error;
|
||||||
axiosError.name = error.name;
|
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);
|
customProps && Object.assign(axiosError, customProps);
|
||||||
return axiosError;
|
return axiosError;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,23 @@ describe('core::AxiosError', function () {
|
|||||||
AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }) instanceof AxiosError
|
AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }) instanceof AxiosError
|
||||||
).toBeTruthy();
|
).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 () {
|
it('should be a native error as checked by the NodeJS `isNativeError` function', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user