gpt4 book ai didi

javascript - axios 请求拦截器错误处理程序中错误对象的形状是什么?

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:11 25 4
gpt4 key购买 nike

技术说明: 由于 axios 针对 Node 和浏览器使用不同的库/机制,因此此问题仅涉及 Node.js axios@0.18 的用法.0.

我可以为 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);
});

当请求拦截器的错误处理程序中描述的回调被触发时,该错误对象的形状是什么?

P.S. 我看到有 this code describing work with errorsaxios 中:

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);
});

request error handler中的error在后面的代码中代表什么?

最佳答案

我认为此源代码可能对您有所帮助:

createError.js

它看起来像 Error 的一个实例, 所以它有error.message, axios 增加了error.config, error.code, error.requesterror.response,根据 enhanceError.js .

关于javascript - axios 请求拦截器错误处理程序中错误对象的形状是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com