diff --git a/lib/adapters/fetch.js b/lib/adapters/fetch.js index e2e90f65..db1795db 100644 --- a/lib/adapters/fetch.js +++ b/lib/adapters/fetch.js @@ -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); } } } diff --git a/test/unit/regression/bugs.js b/test/unit/regression/bugs.js index a9d1509f..f975b162 100644 --- a/test/unit/regression/bugs.js +++ b/test/unit/regression/bugs.js @@ -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(); + } + }); + }); + });