繁体   English   中英

如何使用 Axios 请求拦截器捕获网络错误?

[英]How to catch network errors with Axios request interceptor?

我们的 Vue 应用程序有一些问题,我在 Sentry 日志中注意到 .network 可能不可靠:

Error: Network Error
Error: Request aborted

我想向用户发出警告,但不知道该怎么做。 我尝试使用 Axios 请求拦截器来捕获这些错误,但它们没有用。 有没有人做到这一点?

编辑:

这是不起作用的拦截器。 我还有一个响应拦截器来捕获 403,它工作得很好。

axios.interceptors.request.use(undefined, (err) => {
  // Never gets here for network errors
  return new Promise((resolve, reject) => {
    throw err;
  });
});

你试过了吗?

return Promise.reject(error);

像这样:

 axios.interceptors.response.use(function (response) { // Any status code that lie within the range of 2xx cause this function to trigger // Do something with response data return response; }, function (error) { // Any status codes that falls outside the range of 2xx cause this function to trigger // Do something with response error return Promise.reject(error); });

参考: https://axios-http.com/docs/interceptors

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM