Basic error handling
Handle errors using try/catch or promise.catch():
Error object structure
When a request fails, Axios provides a detailed error object:AxiosError properties
All Axios errors have the following properties:| Property | Type | Description |
|---|---|---|
message | string | Error message summarizing what went wrong |
name | string | Always 'AxiosError' for Axios errors |
code | string | Axios-specific error code (e.g., 'ERR_NETWORK') |
config | object | The request configuration that was used |
request | object | The request object (XMLHttpRequest or http.ClientRequest) |
response | object | The response from the server (if received) |
status | number | HTTP status code (if response was received) |
stack | string | Stack trace of the error |
Error types
Axios categorizes errors into three main types:1. Response errors
The server responded with a status code outside the 2xx range:2. Request errors
The request was made but no response was received:3. Setup errors
An error occurred while setting up the request:Error codes
Axios provides specific error codes to identify issues:| Code | Description |
|---|---|
ERR_BAD_OPTION_VALUE | Invalid value provided in axios configuration |
ERR_BAD_OPTION | Invalid option provided in axios configuration |
ERR_NOT_SUPPORT | Feature or method not supported in the current environment |
ERR_DEPRECATED | Deprecated feature or method used |
ERR_INVALID_URL | Invalid URL provided for request |
ECONNABORTED | Request timed out or aborted by browser |
ERR_CANCELED | Request canceled by user using AbortSignal or CancelToken |
ETIMEDOUT | Request timed out (when transitional.clarifyTimeoutError is true) |
ERR_NETWORK | Network-related issue or CORS/Mixed Content policy violation |
ERR_FR_TOO_MANY_REDIRECTS | Too many redirects |
ERR_BAD_RESPONSE | Response cannot be parsed properly (usually 5xx status) |
ERR_BAD_REQUEST | Request has unexpected format or missing parameters (usually 4xx status) |
Checking error codes
Type checking
isAxiosError
Check if an error is an Axios error:TypeScript support
Handling timeouts
Handle request timeouts specifically:clarifyTimeoutError: