axios: 0.13 Error handling does not handle exceptions

After upgrade from 0.12 to 0.13, catch method does not catch an error, an exception occurs and the whole process aborts.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 10
  • Comments: 20 (5 by maintainers)

Most upvoted comments

@SOSANA There is no ‘extra response object’ in your example.

When an Error is thrown, the response variable will contain the Error. I would suggest to rename that variable to error:

.catch(error => {
})

If a response has been received from the server, the error.response will contain a response object. The data property of the response object will contain the response payload. (See this for details about the response schema.)

.catch(error => {
  if (error.response) {
    // Response has been received from the server
    console.log(error.response.data); // => the response payload 
  }
})

Hope this helps!

Same here. There is some issues with the error object. Previously, we were able to retrieve the error object. We are facing issues now. I am rolling back to the previous version.

Below is the error message I get in the console:

Error: Request failed with status code 404
    at createError (eval at <anonymous> (https://localhost:8080/vendors.js:34:14382), <anonymous>:15:15)
    at settle (eval at <anonymous> (https://localhost:8080/vendors.js:59:22824), <anonymous>:18:12)
    at XMLHttpRequest.handleLoad (eval at <anonymous> (https://localhost:8080/vendors.js:34:48), <anonymous>:75:7)

In the previous version, I used to get the proper error object with status code.

Had this issue

Error: Request failed with status code 422
    at createError (bundle.js:34466)
    at settle (bundle.js:34438)
    at XMLHttpRequest.handleLoad (bundle.js:34311)

Got it fixed by

.catch(error => {
  if (error.response) {
    dispatch(authError(error.response.data.error));
  }
})

@nickuraltsev I followed the examples using .catch(err) but the err is just an exception. Doesn’t contain the returning object like the example shows.

I worked around this by nesting my response call:

.catch(response => {
  dispatch(authError(response.response.data.error));
});

instead of

.catch(response => {
  dispatch(authError(response.data.error));
});

The original error was a type error because response.data didn’t exist.

@mzabriskie

POST https://192.168.1.188:3000/vendor/customerlocation/1 404 (Not Found)
vendor-list.js?2389:57 

Object {data: "Unfortunately, No vendor was found.", status: 404, statusText: "Not Found", headers: Object, config: Object…}

I reverted to 0.12.0 and now I get a proper error object.

nickuraltsev’s answer solve this problem,a hidden error.response.

@alexi21 It’s not the same. You are talking about a documented change mentioned here. In my case the catch method is not even called.