Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions test/unit/regression/bugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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