Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch and handle error response 422 with Redux/Axios?

I have an action making a POST request to the server in order to update a user's password, but I'm unable to handle the error in the chained catch block.

return axios({   method: 'post',   data: {     password: currentPassword,     new_password: newPassword   },   url: `path/to/endpoint` }) .then(response => {   dispatch(PasswordUpdateSuccess(response)) }) .catch(error => {   console.log('ERROR', error)   switch (error.type) {     case 'password_invalid':       dispatch(PasswordUpdateFailure('Incorrect current password'))       break     case 'invalid_attributes':       dispatch(PasswordUpdateFailure('Fields must not be blank'))       break   } }) 

When I log the error this is what I see:

Error Logged

When I check the network tab I can see the response body, but for some reason I can't access the values!

Network Tab

Have I unknowingly made a mistake somewhere? Because I'm handling other errors from different request fine, but can't seem to work this one out.

like image 370
Phillip Boateng Avatar asked Aug 05 '16 22:08

Phillip Boateng


People also ask

How does Axios handle 422 error?

To catch and handle error response 422 with Axios and JavaScript, we call catch with a callback to catch errors. axios . put(url, payload) . then((response) => { // ... }) .

How do you catch errors on Axios?

axios. get('/user/12345') . catch(function (error) { if (error.

How do you get a 422 response?

A 422 status code occurs when a request is well-formed, however, due to semantic errors it is unable to be processed. This HTTP status was introduced in RFC 4918 and is more specifically geared toward HTTP extensions for Web Distributed Authoring and Versioning (WebDAV).


2 Answers

Example

getUserList() {     return axios.get('/users')       .then(response => response.data)       .catch(error => {         if (error.response) {           console.log(error.response);         }       });   } 

Check the error object for response, it will include the object you're looking for so you can do error.response.status

enter image description here

https://github.com/mzabriskie/axios#handling-errors

like image 182
Steve Avatar answered Sep 22 '22 18:09

Steve


Axios is probably parsing the response. I access the error like this in my code:

axios({   method: 'post',   responseType: 'json',   url: `${SERVER_URL}/token`,   data: {     idToken,     userEmail   } })  .then(response => {    dispatch(something(response));  })  .catch(error => {    dispatch({ type: AUTH_FAILED });    dispatch({ type: ERROR, payload: error.data.error.message });  }); 

From the docs:

The response for a request contains the following information.

{   // `data` is the response that was provided by the server   data: {},    // `status` is the HTTP status code from the server response   status: 200,    // `statusText` is the HTTP status message from the server response   statusText: 'OK',    // `headers` the headers that the server responded with   headers: {},    // `config` is the config that was provided to `axios` for the request   config: {} } 

So the catch(error => ) is actually just catch(response => )

EDIT:

I still dont understand why logging the error returns that stack message. I tried logging it like this. And then you can actually see that it is an object.

console.log('errorType', typeof error); console.log('error', Object.assign({}, error)); 

EDIT2:

After some more looking around this is what you are trying to print. Which is a Javascipt error object. Axios then enhances this error with the config, code and reponse like this.

console.log('error', error); console.log('errorType', typeof error); console.log('error', Object.assign({}, error)); console.log('getOwnPropertyNames', Object.getOwnPropertyNames(error)); console.log('stackProperty', Object.getOwnPropertyDescriptor(error, 'stack')); console.log('messageProperty', Object.getOwnPropertyDescriptor(error, 'message')); console.log('stackEnumerable', error.propertyIsEnumerable('stack')); console.log('messageEnumerable', error.propertyIsEnumerable('message')); 
like image 44
Jeroen Wienk Avatar answered Sep 21 '22 18:09

Jeroen Wienk