mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
fix(fetch): treat fetch-related TypeError as an AxiosError.ERR_NETWORK error; (#6380)
This commit is contained in:
@@ -196,13 +196,16 @@ export default isFetchSupported && (async (config) => {
|
||||
} catch (err) {
|
||||
onFinish();
|
||||
|
||||
let {code} = err;
|
||||
|
||||
if (err.name === 'NetworkError') {
|
||||
code = AxiosError.ERR_NETWORK;
|
||||
if (err && err.name === 'TypeError' && /fetch/i.test(err.message)) {
|
||||
throw Object.assign(
|
||||
new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request),
|
||||
{
|
||||
cause: err.cause || err
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
throw AxiosError.from(err, code, config, request);
|
||||
throw AxiosError.from(err, err && err.code, config, request);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -370,4 +370,14 @@ describe('supports fetch with nodejs', function () {
|
||||
|
||||
assert.strictEqual(data, '/?test=1&foo=1&bar=2');
|
||||
});
|
||||
|
||||
it('should handle fetch failed error as an AxiosError with ERR_NETWORK code', async () => {
|
||||
try{
|
||||
await fetchAxios('http://notExistsUrl.in.nowhere');
|
||||
assert.fail('should fail');
|
||||
} catch (err) {
|
||||
assert.strictEqual(String(err), 'AxiosError: Network Error');
|
||||
assert.strictEqual(err.cause && err.cause.code, 'ENOTFOUND');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user