Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle 500 error message with axios

I cannot get the response text in the 500 error with axios.

in Network tab in dev tools i can find the error message like this

{"Message":"500 error message 0","Code":0,"Type":"error"}

and I use

axios()
.then(function(done) {
   //fine and can get done.data
})
.catch(function(error) {
    console.log(error.message);
}); 

but I can only get

Request failed with status code 500

so how can I get the response text with axios

like image 951
COS LIM Avatar asked Jun 13 '19 09:06

COS LIM


People also ask

How do you handle errors in Axios?

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

How do I fix request failed with status code 500?

Reload or Refresh the Webpage Most of the time, the issue is only temporarily and can be corrected by trying the page again. You can use the refresh/reload button, pressing F5 , or by trying the URL again from the address bar.

How do you handle 500 Internal server error in react JS?

To solve 500 HTTP Internal Server Error, reload a web page. You can do that by clicking the refresh/reload button, pressing F5 or Ctrl + R, or trying the URL from the address bar again. The issue might be temporary even if the 500 Internal Server Error is the web server's problem.

Does Axios throw error on 400?

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.


1 Answers

according to axios readme:

axios.get('/user/12345')
  .catch(function (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  });
like image 117
Eugene Ghanizadeh Khoub Avatar answered Oct 02 '22 02:10

Eugene Ghanizadeh Khoub