Skip to content

Commit 696fa75

Browse files
fix: status is missing in AxiosError on and after v1.13.3 (axios#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>
1 parent 569f028 commit 696fa75

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

lib/adapters/fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,14 @@ const factory = (env) => {
247247

248248
if (err && err.name === 'TypeError' && /Load failed|fetch/i.test(err.message)) {
249249
throw Object.assign(
250-
new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request),
250+
new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request, err && err.response),
251251
{
252252
cause: err.cause || err
253253
}
254254
)
255255
}
256256

257-
throw AxiosError.from(err, err && err.code, config, request);
257+
throw AxiosError.from(err, err && err.code, config, request, err && err.response);
258258
}
259259
}
260260
}

test/unit/regression/bugs.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,56 @@ describe('issues', function () {
4040
}
4141
});
4242
});
43+
44+
describe('7364', function () {
45+
it('fetch: should have status code in axios error', async function () {
46+
const isFetchSupported = typeof fetch === 'function';
47+
if (!isFetchSupported) {
48+
this.skip();
49+
}
50+
51+
const server = http.createServer((req, res) => {
52+
res.statusCode = 400;
53+
res.end();
54+
}).listen(0);
55+
56+
const instance = axios.create({
57+
baseURL: `http://localhost:${server.address().port}`,
58+
adapter: "fetch",
59+
});
60+
61+
try {
62+
await instance.get("/status/400");
63+
} catch (error) {
64+
assert.equal(error.name, "AxiosError");
65+
assert.equal(error.isAxiosError, true);
66+
assert.equal(error.status, 400);
67+
} finally {
68+
server.close();
69+
}
70+
});
71+
72+
it('http: should have status code in axios error', async function () {
73+
const server = http.createServer((req, res) => {
74+
res.statusCode = 400;
75+
res.end();
76+
}).listen(0);
77+
78+
const instance = axios.create({
79+
baseURL: `http://localhost:${server.address().port}`,
80+
adapter: "http",
81+
});
82+
83+
try {
84+
await instance.get("/status/400");
85+
} catch (error) {
86+
assert.equal(error.name, "AxiosError");
87+
assert.equal(error.isAxiosError, true);
88+
assert.equal(error.status, 400);
89+
} finally {
90+
server.close();
91+
}
92+
});
93+
});
94+
4395
});

0 commit comments

Comments
 (0)