Guides

Error handling guide

How to distinguish network failures, timeouts, HTTP errors and cancellations.

Every failure rejects with an AxiosError. Four situations are worth telling apart.

Situationerror.responseerror.requestTypical error.code
Server answered with an error statussetsetERR_BAD_REQUEST, ERR_BAD_RESPONSE
Request never got a responseunsetsetERR_NETWORK, ECONNABORTED, ETIMEDOUT
Request was cancelledunsetmaybe setERR_CANCELED
The request was misconfiguredunsetunsetERR_BAD_OPTION, ERR_BAD_URL, ERR_NOT_SUPPORT
import axios from "axios";

axios.get("/x").catch((error) => {
  if (axios.isCancel(error)) return;             // cancelled on purpose
  if (error.response) {
    console.error(error.response.status);        // the server answered
    return;
  }
  console.error(error.code, error.message);      // transport-level failure
});

See errors for the complete code list parsed from the pinned source.