Member-only story
How are exceptions handled in Axios?
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:
- Config: Request configuration
- Headers: Response header data (AxiosHeader object)
- Request: Request an instance. The browser environment is the XMLHttpRequest object
- Status: HTTP status code. This case is 200, indicating that the request has been successfully processed
- 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)
})