admin管理员组文章数量:1415491
Our Vue application had some problems and I noticed in Sentry logs that probably the network has been unreliable:
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. Has anyone acplished this?
EDIT:
This is the interceptor that didn't work. I also have a response interceptor to catch 403 and it works perfectly.
axios.interceptors.request.use(undefined, (err) => {
// Never gets here for network errors
return new Promise((resolve, reject) => {
throw err;
});
});
Our Vue application had some problems and I noticed in Sentry logs that probably the network has been unreliable:
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. Has anyone acplished this?
EDIT:
This is the interceptor that didn't work. I also have a response interceptor to catch 403 and it works perfectly.
axios.interceptors.request.use(undefined, (err) => {
// Never gets here for network errors
return new Promise((resolve, reject) => {
throw err;
});
});
Share
Improve this question
edited Feb 18, 2022 at 10:11
tputkonen
asked Feb 18, 2022 at 9:18
tputkonentputkonen
5,74918 gold badges66 silver badges91 bronze badges
3
- What exactly did you try? – Estus Flask Commented Feb 18, 2022 at 9:43
- @EstusFlask I edited the question with a sample code. – tputkonen Commented Feb 18, 2022 at 10:11
-
2
Why is it request interceptor? You need to handle a response. If you handle Axios errors consistently, you won't get unhandled errors. Network error is detected like
!error.status && error.message === 'Network Error'
. – Estus Flask Commented Feb 18, 2022 at 10:29
1 Answer
Reset to default 1Did 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./docs/interceptors
本文标签: javascriptHow to catch network errors with Axios request interceptorStack Overflow
版权声明:本文标题:javascript - How to catch network errors with Axios request interceptor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745152353a2644976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论