[英]What is the shape of error object inside axios request interceptor error handler?
Technical note: As axios uses different libraries/mechanisms for Node and browser, this question touches only Node.js
usage of axios@0.18.0
.技术说明:由于 axios 对 Node 和浏览器使用不同的库/机制,这个问题只涉及
axios@0.18.0
Node.js
用法。
I can set up the following interceptor for the axios
library ( https://github.com/axios/axios#interceptors ):我可以为
axios
库( https://github.com/axios/axios#interceptors )设置以下拦截器:
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
}, function (error) {
// Do something with request error
//
// I am asking about this error handler and this error object
//
return Promise.reject(error);
});
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
// Do something with response error
return Promise.reject(error);
});
When the callback described in error handler for request interceptor is triggered and and what is the shape of that error object?当请求拦截器的错误处理程序中描述的回调被触发时,该错误对象的形状是什么?
PS I see that there is this code describing work with errors in axios
: PS我看到有这段代码描述了
axios
错误:
axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
//
//
// !!!! This is a request error handler !!!
//
//
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
What will the error
inside the request error handler represent in the latter code?会出现什么
error
的请求错误处理程序中的代码,后者代表什么呢?
I think this source code might help you:我认为此源代码可能对您有所帮助:
It looks like an instance of Error , so it has error.message
, and axios
adds error.config
, error.code
, error.request
and error.response
, per enhanceError.js .它看起来像Error 的一个实例,所以它有
error.message
,并且axios
添加了error.config
、 error.code
、 error.request
和error.response
,每个enhanceError.js 。
When the callback described in error handler for request interceptor is triggered and and what is the shape of that error object?
当请求拦截器的错误处理程序中描述的回调被触发时,该错误对象的形状是什么?
the error handler (.catch clause) will be triggered by the interceptor, when it "rejects" a promise like in this part of your code:错误处理程序(.catch 子句)将被拦截器触发,当它“拒绝”像您代码的这一部分那样的承诺时:
axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
// Do something with response error
return Promise.reject(error); // <---- HERE
});
The shape of the axios error object is a JSON object as stated on the handling error section of axios docs on github axios 错误对象的形状是 JSON 对象,如github 上 axios 文档的处理错误部分所述
What will the error inside the request error handler represent in the latter code?
请求错误处理程序中的错误在后面的代码中代表什么?
It will be the error response from the request bypassed by your axios interceptor这将是您的 axios 拦截器绕过的请求的错误响应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.