Describe the bug
In Node.js, if the server sends headers but aborts the stream before completing the body, Axios throws an error without populating err.response, even though response metadata like status, statusText, and headers was already received. Axios currently discards this information entirely, making it inaccessible to the application. As a result, consumers cannot programmatically handle these errors based on the response metadata, which can be critical for debugging, retry logic, or user messaging.
To Reproduce
- Create a Node.js server that sends headers and then immediately closes the connection.
- Use Axios to make a request to this server.
- Catch the error and inspect
err.response.
Code snippet
// test-server.cjs
const http = require('http');
http.createServer((req, res) => {
console.log('[SERVER] Got request');
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('Partial response');
setTimeout(() => {
console.log('[SERVER] Destroying connection...');
// res.end(); // --> header is captured as expected.
res.destroy(); // --> (Bug) If aborted, header is not captured and not accessible.
}, 100); // 100ms delay
}).listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
// test-client.cjs
const axios = require('axios');
axios.get('http://localhost:3000')
.then(res => {
console.log('Success?', res.status);
})
.catch((err) => {
console.log('[CLIENT] err.message:', err.message);
console.log('[CLIENT] err.code:', err.code);
console.log('[CLIENT] err.response:', err.response); // Bug, it is undefined
if (err.response) {
console.log('[CLIENT] err.response.status:', err.response.status);
console.log('[CLIENT] err.response.data:', err.response.data);
}
});
Then run both servers
Expected behavior
err.response should contain the response from the server in case of stream abort.
Axios Version
v1.9.0
Node.js Version
v22
Describe the bug
In Node.js, if the server sends headers but aborts the stream before completing the body, Axios throws an error without populating
err.response, even though response metadata likestatus,statusText, andheaderswas already received. Axios currently discards this information entirely, making it inaccessible to the application. As a result, consumers cannot programmatically handle these errors based on the response metadata, which can be critical for debugging, retry logic, or user messaging.To Reproduce
err.response.Code snippet
Then run both servers
Expected behavior
err.responseshould contain the response from the server in case of stream abort.Axios Version
v1.9.0
Node.js Version
v22