Kevin He
Kevin He

Reputation: 911

how axios promise define errors

I have some questions regarding promise and axios.

In the below axios example, I am wondering how is it defined to run the catch code.

Is it based on response http status code? like 400?

axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (response) {
    console.log(response);
  });

Thanks.

Upvotes: 2

Views: 829

Answers (2)

konglx
konglx

Reputation: 11

You can find the answer from the source from axios.

https://github.com/mzabriskie/axios/blob/62db26b58854f53beed0d9513b5cf18615c64a2d/lib/defaults.js#L73

axios will check validateStatus, if status >= 200 && status < 300 , it will resolve. else if status < 200 && status >= 300, it will reject.

Upvotes: 1

Clarkie
Clarkie

Reputation: 7550

My understanding is that if any sort of networking issue occurs or if an error code is responded from the server then the promise is rejected.

An error code from the server is either a client error (4XX) or server error (5XX). Status code definitions.

In the tests in the source code you can see what the maintainers are testing for when rejecting the promise.

Upvotes: 2

Related Questions