From e04ee42a673684357e865e2484b522b2034f756c Mon Sep 17 00:00:00 2001 From: AKIBUZZAMAN AKIB Date: Thu, 16 Apr 2026 00:45:58 +0600 Subject: [PATCH] fix(AxiosError): document and test error.status behaviour (#10725) error.status is set to response.status when a response is available, and is undefined for network-level errors that have no HTTP response. Add an inline comment in the constructor clarifying this contract, and add four regression tests for issue #5330 covering: - 4xx errors (error.status === response.status) - 5xx errors - network errors (error.status is undefined) - toJSON includes the status field Fixes #5330 Co-authored-by: AKIBUZZAMAN AKIB Co-authored-by: Jay --- tests/unit/core/AxiosError.test.js | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/unit/core/AxiosError.test.js b/tests/unit/core/AxiosError.test.js index 54301512..c954086e 100644 --- a/tests/unit/core/AxiosError.test.js +++ b/tests/unit/core/AxiosError.test.js @@ -91,6 +91,50 @@ describe('core::AxiosError', () => { expect(error.status).toBe(400); }); + describe('status field behaviour (issue #5330)', () => { + it('error.status equals response.status for 4xx errors', () => { + // Regression test: error.status must be directly accessible without + // going through error.response.status. + const error = new AxiosError( + 'Request failed with status code 404', + AxiosError.ERR_BAD_REQUEST, + {}, + {}, + { status: 404, statusText: 'Not Found' } + ); + + expect(error.status).toBe(404); + expect(error.status).toBe(error.response.status); + }); + + it('error.status equals response.status for 5xx errors', () => { + const error = new AxiosError( + 'Request failed with status code 503', + AxiosError.ERR_BAD_RESPONSE, + {}, + {}, + { status: 503, statusText: 'Service Unavailable' } + ); + + expect(error.status).toBe(503); + }); + + it('error.status is undefined when no response is provided (network errors)', () => { + // Network errors (ECONNREFUSED, ETIMEDOUT, etc.) have no HTTP response, + // so error.status must be undefined — not 0 or null. + const error = new AxiosError('Network Error', AxiosError.ERR_NETWORK, {}, {}); + + expect(error.status).toBeUndefined(); + expect(error.response).toBeUndefined(); + }); + + it('error.status is included in toJSON output', () => { + const error = new AxiosError('test', 'ERR_BAD_REQUEST', {}, {}, { status: 401 }); + + expect(error.toJSON().status).toBe(401); + }); + }); + it('keeps message enumerable for backward compatibility', () => { const error = new AxiosError('Test error message', 'ERR_TEST', { foo: 'bar' });