Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catching error body using axios post

I am sending a status code 422 from my backend code with response body which contains the description of the error. I am using axios post as below to post a request:

post: function(url, reqBody) {     const request = axios({         baseURL: config.apiUrl,         url: url,         headers: {             'Content-Type': 'application/json',             'Authorization': sessionStorage.getItem('token')         },         method: 'POST',         data: reqBody,         responseType: 'json'     });     return request         .then((res) => {             return res;         })         .catch((error) => {             console.log(error);             return error;         }) } 

The problem is when backend is returning error code 422, the error object I am catching has no information about response body. Is there any way I can retrieve the error text?

like image 600
Amol Aggarwal Avatar asked Jul 10 '17 17:07

Amol Aggarwal


People also ask

How do you catch error in Axios post?

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

Does Axios catch 400 error?

By default, the axios HTTP library throws an error anytime the destination server responds with a 4XX / 5XX error (for example, a 400 Bad Request ). Since axios raises an error, your workflow will stop at this step.

How do you send a body request in Axios?

To perform an HTTP POST request in Axios, call axios. post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.

What is the body of an Axios error response?

The "body" of an AXIOS error response depends from the type of response the request had. If you would like full details about this issue you can see this blogpost: How to catch the body of an error in AXIOS. In summary AXIOS will return 3 different body depending from the error:

What happens if there is no catch in an Axios call?

Without a catch in your Axios call, you won't get a response unless the response status is 200. Any other response status you will only get an error in your browser console like the image below: Without a catch, you can't alert the user that there's an issue and the user doesn't know what the problem is.

Why am I not receiving the expected status code in Axios?

If you are not receiving the expected status code, you might change the way you check the response in the interceptor. You can check any of the elements that Axios response is structured. Thanks for contributing an answer to Stack Overflow!

How to check if a password is correct using Axios?

If we have a catch in the axios call we can send back json with the error code, and use it to inform the user that 'Authentication failed' by showing an alert. Now the user can check their inputs for errors. If we find a user in the database we can move forward to checking if the password is correct.


2 Answers

I had this same issue and the answer (as per Axios >= 0.13) is to specifically check error.response.data:

axios({     ... }).then((response) => {     .... }).catch((error) => {     if( error.response ){         console.log(error.response.data); // => the response payload      } }); 

See here for more details.

like image 156
JoeTidee Avatar answered Sep 24 '22 10:09

JoeTidee


The "body" of an AXIOS error response depends from the type of response the request had.

If you would like full details about this issue you can see this blogpost: How to catch the body of an error in AXIOS.

In summary AXIOS will return 3 different body depending from the error:

  1. Wrong request, we have actually done something wrong in our request (missing argument, bad format), that is has not actually been sent. When this happen, we can access the information using error.message.

    axios.get('wrongSetup') .then((response) => {}) .catch((error) => {     console.log(error.message); }) 
  2. Bad Network request: This happen when the server we are trying to reach does not respond at all. This can either be due to the server being down, or the URL being wrong. In this case, we can access the information of the request using error.request.

    axios.get('network error') .then((response) => {}) .catch((error) => {     console.log(error.request ); }); 
  3. Error status: This is the most common of the request. This can happen with any request that returns with a status that is different than 200. It can be unauthorised, not found, internal error and more. When this error happen, we are able to grasp the information of the request by accessing the parameter specified in the snippets below. For the data (as asked above) we need to access the error.response.data.

    axios.get('errorStatus') .then((response) => {}) .catch((error) => {       console.log(error.response.data);        console.log(error.response.status);        console.log(error.response.headers);   }) 
like image 37
Zelig880 Avatar answered Sep 21 '22 10:09

Zelig880