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>
This commit is contained in:
Willian Agostini 2026-02-04 04:47:12 -03:00 committed by GitHub
parent 569f028a58
commit 696fa753c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 2 deletions

View File

@ -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);
}
}
}

View File

@ -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();
}
});
});
});