Understanding the Axios Error Object Structure

When an HTTP request fails in the Axios library, it rejects the promise and returns an AxiosError object containing detailed diagnostic information about the failure. Understanding the structure of this error object is essential for effective error handling, debugging, and providing meaningful feedback to users. This article outlines the primary properties of the Axios error object, breaks down the nested response payload, and explains how to inspect errors across different failure scenarios.

The Top-Level AxiosError Object

An AxiosError extends the standard JavaScript Error object and includes several key properties:


The error.response Structure

When a request is successfully received by the server but results in an HTTP 4xx or 5xx status code, Axios populates the error.response property with the following structure:


Categorizing Error Scenarios

Axios errors generally fall into three distinct categories based on which properties are populated:

  1. Server Responded with Error (error.response is defined): The request was received, but the server returned a non-2xx status code. Inspect error.response.status and error.response.data to handle these errors.
  2. No Response Received (error.request is defined, error.response is undefined): The request was made, but no response was received (e.g., network drop, CORS issue, or server timeout).
  3. Request Setup Error (error.request and error.response are undefined): An error occurred while setting up the request configuration before it could be sent. Inspect error.message for details.