Member-only story

How are exceptions handled in Axios?

Beck Moulton
10 min readJan 11, 2025

--

Normal requests in Axios

When the request service returns normally in Axios, it falls into the. then() method.

axios.get('https://httpstat.us/200')
.then(res => {
console.log(res)
})

The effect is as follows:

Axios will wrap the response result in the data property of the returned Response object, in addition to:

  1. Config: Request configuration
  2. Headers: Response header data (AxiosHeader object)
  3. Request: Request an instance. The browser environment is the XMLHttpRequest object
  4. Status: HTTP status code. This case is 200, indicating that the request has been successfully processed
  5. StatusText: Text description of the status code

Abnormal requests in Axios

There are two types of abnormal requests in Axios: responsive abnormal requests and unresponsive abnormal requests.

Responsive exceptions

When the returned HTTP status code is a dog outside of 2xx, it will enter axios’. match() method.

403 response:

axios.get('https://httpstat.us/403')
.catch(err => {
console.log(err)
})

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet