Skip to content

Commit

Permalink
fix(fetch): treat fetch-related TypeError as an AxiosError.ERR_NETWOR…
Browse files Browse the repository at this point in the history
…K error; (#6380)
  • Loading branch information
DigitalBrainJS committed May 7, 2024
1 parent 81e0455 commit bb5f9a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
10 changes: 10 additions & 0 deletions test/unit/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
});

0 comments on commit bb5f9a5

Please sign in to comment.