mirror of
https://github.com/axios/axios.git
synced 2026-04-11 14:21:59 +08:00
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:
parent
569f028a58
commit
696fa753c5
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user