Axios Behavior on Abrupt Server Disconnection
When a target server abruptly closes a connection, the Axios HTTP
client fails the active request and rejects the returned Promise with an
AxiosError. Because the connection is terminated before the
server can return valid HTTP headers or a complete response body, Axios
receives no HTTP status code and leaves the response object undefined.
Handling this failure requires inspecting low-level network error codes,
understanding the differences between Node.js and browser runtimes, and
implementing appropriate recovery mechanisms like retries or
timeouts.
The Lifecycle of an Abrupt Termination
When an HTTP client initiates a request, it opens a TCP socket and awaits the server's response. An abrupt closure happens when the server terminates this TCP connection prematurely—often sending a TCP RST (reset) packet or a FIN packet before completing the HTTP response cycle.
Because the HTTP exchange was not concluded:
- No HTTP status code (such as 200, 500, or 502) is generated by the server.
- The Axios Promise rejects immediately and enters the
.catch()block or throws an exception inasync/awaitsyntax. - The
error.responseproperty remainsundefined.
Runtime-Specific Error Signatures
Axios behaves differently depending on whether it is running in a Node.js runtime or within a web browser.
Node.js Environment
In Node.js, Axios uses the native http and
https modules. When the server abruptly drops the
connection, the socket emits an error event that Axios maps into the
error object:
error.code: Typically set to'ECONNRESET'(Connection Reset by Peer) or'ERR_SOCKET_HANGUP'.error.message: Commonly displayssocket hang uporread ECONNRESET.error.request: Contains the Node.jsClientRequestinstance.
Browser Environment
In web browsers, Axios relies on the XMLHttpRequest or
fetch APIs. For security and architectural reasons,
browsers do not expose raw TCP socket errors to JavaScript:
error.code: Commonly'ERR_NETWORK'.error.message: Generic message statingNetwork Error.error.request: Contains the browser'sXMLHttpRequestinstance.
Inspecting the Error Object
To identify an abrupt disconnection in an application, inspect the
properties of the caught AxiosError:
try {
const response = await axios.get('https://api.example.com/data');
} catch (error) {
if (axios.isAxiosError(error)) {
if (!error.response && error.request) {
// The request was made, but no response was received (abrupt close or network drop)
console.error('Connection terminated abruptly:', error.code, error.message);
} else if (error.response) {
// The server responded with an HTTP status code outside the 2xx range
console.error('Server responded with status:', error.response.status);
}
}
}Best Practices for Mitigation
- Configuring Timeouts: An abrupt closure usually
fails immediately, but hanging sockets can stall indefinitely. Always
define a
timeoutin the Axios configuration to prevent unbounded waits. - Automatic Retries: For idempotent operations (like
GETorPUT), use interceptors or libraries likeaxios-retryto automatically retry requests when encounteringECONNRESETor network errors. - Server Keep-Alive Management: Ensure that load balancers, proxies (e.g., NGINX), and application servers have coordinated TCP Keep-Alive and idle timeout configurations to prevent race conditions where a server closes a connection just as Axios sends a request.