Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The error handling function in request interceptors may not work as expect. #4537

Open
Tackoil opened this issue Mar 20, 2022 · 4 comments · May be fixed by #6269
Open

The error handling function in request interceptors may not work as expect. #4537

Tackoil opened this issue Mar 20, 2022 · 4 comments · May be fixed by #6269

Comments

@Tackoil
Copy link

Tackoil commented Mar 20, 2022

Describe the issue

As the usage in README.md, a request interceptors will be like:

axios.interceptors.request.use(function onFulfilled(config) {
    // Do something before request is sent
    return config;
  }, function onRejected(error) {
    // Do something with request error
    return Promise.reject(error);
  });

However, the onRejected function will handle the error in the next request intercetor onFulfilled.

Example Code

instance.interceptors.request.use((config) => {
    throw new Error("Error in req1");
    return config;
}, (error) => {
    console.log("req1 handle the error", error);
    return Promise.reject(error);
})

instance.interceptors.request.use((config) => {
    throw new Error("Error in req2");
    return config;
}, (error) => {
    console.log("req2 handle the error", error);
    return Promise.reject(error);
})

instance.get("some site").then((res) => {
    console.log("get res")
}, (err) => {
    console.log("get err", err);
})

The output in console will be:

req1 handle error Error: Error in req2
get err Error: Error in req2

Expected behavior, if applicable

The better behavior may be that the error will be handled by function in the same request interceptor, I think.

Environment

  • Axios Version: 0.26.1
  • Adapter: HTTP
  • Node.js Version :16.14.0
  • OS: Windows 10

Additional context/Screenshots

The following graph was drawn according to the source code. It may be helpful.

Behavior in Source Code

@luke358
Copy link

luke358 commented Mar 30, 2022

use synchronousRequestInterceptors, onFuillfilled2 ---> onReject2

@luke358
Copy link

luke358 commented Mar 30, 2022

@Tackoil
Copy link
Author

Tackoil commented Mar 30, 2022

use synchronousRequestInterceptors, onFuillfilled2 ---> onReject2

Thank you for your reply. It's my fault that I forgot to explain it'll only happen with asynchronous interceptors. But interceptors are presumed to be asynchronous by default and the behavior above is unreasonable, specially onRejected2 has no chance of being work. So I think it need a fix.

@Anatoliy13Kravchenko
Copy link

Anatoliy13Kravchenko commented Apr 9, 2023

Hi there.
Unfortunately, I've met the same issue and it took a while to figure out, what's the problem.
If you aren't going to solve the issue with asynchronous interceptors, may you at least highlight this in your documentation, thus you will save a lot of time for developers like me.
Thank you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants