The exception thrown axios interceptors to intercept interface,
Ask you a question
Data interface to return to normal or abnormal return error when data can be intercepted, throw an exception but interface cannot be intercepted, only in a single interface call there can be intercepted,
Leng in response and the error print out
This is what?
Request. Js code:
The import axios from "axios
"The import {
The Message,
MessageBox
} the from "element - UI"
The import store from "../store/store "
Import the router from "./../router "
The import {
GetToken
} the from "@/utils/auth"
The import i18n from '../i18n/i18n '
Import errorCode from "@/utils/errorCode. Js"//error code unified handling
Const projectCode=localStorage. The getItem (" projectCode ");
//create axios instance
Const service=axios. Create ({
BaseURL: process. The env. BASE_API,//API base_url
Timeout: 15000//request timeout
})
//request interceptor
Service. The interceptors. Request. Use (
The config=& gt; {
If (localStorage. The getItem (" token ")) {
Config. Headers [" Authorization "]=localStorage. The getItem (" token ")//make each request with a custom token please according to the actual situation to modify
}
Return the config
},
Error=& gt; {
Promise. Reject (error)
}
)
//respone interceptor
Service. The interceptors. Response. Use (
The response=& gt; {
The console. The log (" respone blocker response ", the response)
Let responseCode=response. The data. The code;
If (responseCode! {
="200")Let the message=errorCode. ErrorMessage [responseCode];
The Message ({
Message: the message,
Type: "error",
Duration: 3 * 1000,
Center: projectCode=="Chesapeake"
})
}
return response;
},
Error=& gt; {
The console. The log (" respone blocker error, error. The response)
Let the message="";
If (error & amp; & Error. The response) {//cross-domain exist for less than a status code
the condition of theMessage=errorCode. ErrorStatus [error. The response. The status] + ", "+ i18n. T (' option. ErrCode) +" : "+ error. The response. The status
} else {//cross domain for less than a status code or other status code for handling
Message=i18n. T (' option.net workErr ')
}
The Message ({
Message: the message,
Type: "error",
Duration: 3 * 1000,
Center: projectCode=="Chesapeake"
})
Return Promise. Resolve (error. The response);
}
)
Export the default service