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

How to trigger interceptors request error ? #2509

Closed
ch-zgh-1993 opened this issue Nov 1, 2019 · 5 comments
Closed

How to trigger interceptors request error ? #2509

ch-zgh-1993 opened this issue Nov 1, 2019 · 5 comments
Assignees

Comments

@ch-zgh-1993
Copy link

which config for request, can tirgger interceptors request error ?

thanks for your answer!

@yasuf
Copy link
Collaborator

yasuf commented Nov 3, 2019

That request interceptor error will be triggered when there's an error with the interceptor, you can test this by doing:

    axios.interceptors.request.use(
      () => {
        throw new Error("My error");
      },
      () => {
        debugger;
      }
    );
    axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then(res => {
        const persons = res.data;
        this.setState({ persons });
      });

You get to the debugger because there's an error triggered in the interceptor

@yasuf yasuf self-assigned this Nov 3, 2019
@yasuf yasuf closed this as completed Nov 5, 2019
@ch-zgh-1993
Copy link
Author

The error is not catched and not run debugger... But when register debugger interceptor.request first, and then register other interceptor.request to throw new Error, the debugger is triggered. Can you tell me why? what wrong things with i trying?

axios.interceptors.request.use(
  () => {
    // throw new Error('My error')
  },
  () => {
  	debugger
  }
)
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    throw new Error('My error')
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

thanks for your answer!

@chinesedfan
Copy link
Collaborator

@ch-zgh-1993 What you need is some practices about promises. Good luck!

axios.interceptors.request.use(f1, f2)
axios.interceptors.request.use(f3, f4)

// is much similar with
promise.then(f1, f2).then(f3, f4)

If you are familiar with Promises, you will know that f2 can't catch errors in f1, but f4 can.
And f4 can't catch errors in f3.

@ch-zgh-1993
Copy link
Author

I know this. but its right design about catch request.use error? if only one reques.use, what effect about debugger ?

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

@chinesedfan
Copy link
Collaborator

See #1556. Someone has the same curiosity with you.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants