简体   繁体   English

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

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

Our Vue application had some problems and I noticed in Sentry logs that probably the.network has been unreliable:我们的 Vue 应用程序有一些问题,我在 Sentry 日志中注意到 .network 可能不可靠:

Error: Network Error
Error: Request aborted

I would like to show warning to the user, but couldn't figure out how to do it.我想向用户发出警告,但不知道该怎么做。 I tried to catch these errors using Axios request interceptor, but they didn't work.我尝试使用 Axios 请求拦截器来捕获这些错误,但它们没有用。 Has anyone accomplished this?有没有人做到这一点?

EDIT:编辑:

This is the interceptor that didn't work.这是不起作用的拦截器。 I also have a response interceptor to catch 403 and it works perfectly.我还有一个响应拦截器来捕获 403,它工作得很好。

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

Did you try?你试过了吗?

return Promise.reject(error);

like this:像这样:

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

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

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

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