From 696fa753c5366afbd21859c294c64c9ff2b359ab Mon Sep 17 00:00:00 2001 From: Willian Agostini Date: Wed, 4 Feb 2026 04:47:12 -0300 Subject: [PATCH] fix: status is missing in AxiosError on and after v1.13.3 (#7368) * test: add error handling tests for fetch and http adapters with status code * fix: improve error handling in fetch adapter by including request and response in AxiosError * fix: skip fetch test if fetch is not supported * Update lib/adapters/fetch.js Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix: improve error handling in fetch adapter by using the correct request object --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- lib/adapters/fetch.js | 4 +-- test/unit/regression/bugs.js | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/adapters/fetch.js b/lib/adapters/fetch.js index e2e90f65..db1795db 100644 --- a/lib/adapters/fetch.js +++ b/lib/adapters/fetch.js @@ -247,14 +247,14 @@ const factory = (env) => { if (err && err.name === 'TypeError' && /Load failed|fetch/i.test(err.message)) { throw Object.assign( - new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request), + new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request, err && err.response), { cause: err.cause || err } ) } - throw AxiosError.from(err, err && err.code, config, request); + throw AxiosError.from(err, err && err.code, config, request, err && err.response); } } } diff --git a/test/unit/regression/bugs.js b/test/unit/regression/bugs.js index a9d1509f..f975b162 100644 --- a/test/unit/regression/bugs.js +++ b/test/unit/regression/bugs.js @@ -40,4 +40,56 @@ describe('issues', function () { } }); }); + + describe('7364', function () { + it('fetch: should have status code in axios error', async function () { + const isFetchSupported = typeof fetch === 'function'; + if (!isFetchSupported) { + this.skip(); + } + + const server = http.createServer((req, res) => { + res.statusCode = 400; + res.end(); + }).listen(0); + + const instance = axios.create({ + baseURL: `http://localhost:${server.address().port}`, + adapter: "fetch", + }); + + try { + await instance.get("/status/400"); + } catch (error) { + assert.equal(error.name, "AxiosError"); + assert.equal(error.isAxiosError, true); + assert.equal(error.status, 400); + } finally { + server.close(); + } + }); + + it('http: should have status code in axios error', async function () { + const server = http.createServer((req, res) => { + res.statusCode = 400; + res.end(); + }).listen(0); + + const instance = axios.create({ + baseURL: `http://localhost:${server.address().port}`, + adapter: "http", + }); + + try { + await instance.get("/status/400"); + } catch (error) { + assert.equal(error.name, "AxiosError"); + assert.equal(error.isAxiosError, true); + assert.equal(error.status, 400); + } finally { + server.close(); + } + }); + }); + });