If the Axios request fails, how to get the status code and error information returned by the interface? How to encapsulate the function to handle the common error code?
The method is as follows
1. Use the object to map the status code to the corresponding prompt
const codeMessage = {
200: 'The server successfully returned the requested data.' ,
201: 'New or modified data was successful.' ,
202: 'A request has been queued in the background (asynchronous task).' ,
204: 'Deleting data was successful.' ,
400: 'There was an error in the outgoing request, the server did not perform a new or modified data operation.' ,
401: 'The user does not have permission (wrong token, username, password).' ,
403: 'The user is authorized, but access is forbidden.' ,
404: 'The request was sent for a record that does not exist and the server did not perform the operation.' ,
406: 'The format of the request was not available.' ,
410: 'The requested resource was permanently deleted and will not be available again.' ,
422: 'A validation error occurred when an object was created.' ,
500: 'An error occurred on the server, please check the server.' ,
502: 'Gateway error.' ,
503: 'Service is not available, the server is temporarily overloaded or under maintenance.' ,
504: 'Gateway timeout.',
};
2. Encapsulate the common error request function
function errorHandle(error) {
if (error.response) {
// The request was made and the server responded with a status code
// The request has been sent and the server responds with a status code
// that falls out of the range of 2xx out of the range of 2xx
const { status } = error.response;
if(status){
const errorText = codeMessage[status]
// notification is an Ant Design Anthem component and needs to be replaced with a self-defined component
notification.error({
message: `Error code ${status}`,
description: `${
errorText
}`,
duration: 2.5
});
}
} else {
notification.error({
message: 'request error!',
description: '',
duration: 2.5
});
}
}
3. Use error request function in common request response interceptor (recommended)
axios.interceptors.response.use(
function(){
// Interface access success public interceptor
},
function(error){
// Interface access failure public interceptor
errorHandle(error)
})
4. Using the wrong request function in a request (not recommended)
axios.get('api/xxxx').then(res => {
console.log(res); // The request succeeds in returning data
}).catch(errorHandle); // errorHandle is a wrapped public callback
5. The error parameter in Axios catch contains
error.response
error.response.headers
error.response.status //Status code error.response.data
error.request
error.message
error.config
Wait a minute…
Read More:
- [Solved] Axios Error: Request body larger than maxBodyLength limit
- Jenkins Error: code=exited, status=127 [How to Solve]
- Fetch Error: Failed to execute ‘fetch’ on ‘Window’: Request with GET/HEAD method cannot have body
- [Solved] “error_code“:500,“message“:“IO Error trying to forward REST request: java.net.ConnectException: Connection Refused
- (How to Fix) Ora-00600: internal error code, arguments: [4194]
- POI Export Excel Error: HTTP Status 500 – Request processing failed; nested exception is java.lang.NullPointerException
- How to Solve Gazebo code 127
- How to Solve Maven Error: Return code is: 501 , ReasonPhrase:HTTPS Required.
- IDEA: How to Solve spark source code Modified Error
- How to Fix Failed to load resource: the server responded with a status of 404()
- There is no error when compiling and packaging the IDEA code, but the code displays an error
- Failed to create pod sandbox: rpc error: code = Unknown desc = [failed to set up sandbox container…
- [Solved] JSON parse error: Unexpected character (‘‘‘ (code 39)): was expecting double-quote to start ……
- Git pull code error fatal: authentication failed fo
- How to Solve Android wechat payment Error: error_code = -1
- [Solved] Failed to connect to server(code:1006)
- [Solved] ENSP Failed to Start AR Device error code: 40
- [Solved] “HttpSendRequest“ failed, Windows error code=12002 andretry limit (0) exceeded for URL
- [step on the pit] idea submits the code to GitHub and prompts 403 — the requested URL returned error: 403
- django Internal server error 500 Modify source code to view print errors