Skip to content

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

@Tackoil

Description

@Tackoil

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions