examples/improved-network-errors.md
This document explains a simple approach to make Axios network errors more helpful and human-readable.
By default, Axios shows a generic "Network Error" message for many failures.
This can be confusing because it doesn't explain "what actually went wrong" (e.g., no internet, a timeout, a CORS issue, etc.).
Our approach adds clear, categorised error messages for different network issues.
==> Problem
Axios currently throws the same Network Error message for many different cases:
These cases all look the same to developers and users, making debugging harder.
--> Our Approach — Wrapper / Middleware
We created a small wrapper function called enhanceNetworkError() that:
error.code, error.message, and response status.error.detailedMessage with a short, clear explanation.error.code (like ERR_TIMEOUT, ERR_DNS_FAILURE, etc.).The wrapper is used inside an Axios instance via a Response interceptor.
-> How It Works
enhanceNetworkError() function checks what type of error it is:
ERR_NO_INTERNETERR_DNS_FAILUREERR_TIMEOUTERR_CORS_BLOCKEDERR_SERVERERR_CLIENTERR_NETWORK_GENERICcode and detailedMessage.-> Example Usage
const api = createEnhancedClient({ baseURL: 'https://example.com' });
api
.get('/data')
.then((res) => console.log(res.data))
.catch((err) => {
console.error(err.code); // e.g., ERR_TIMEOUT
console.error(err.detailedMessage); // e.g., "The request took too long to respond."
});