npmerror-handling95% confidence\u2191 91

Axios. How to get error response even when api return 404 error, in try catch finally

Full error message
for e.g.

(async() => {
  let apiRes = null;
  try {
    apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a');
  } catch (err) {
    console.error(err);
  } finally {
    console.log(apiRes);
  }
})();

in finally, apiRes will return null. 

Even when the api get a 404 response, there is still useful information in the response that I would like to use.

How can I use the error response in finally when axios throws error.

https://jsfiddle.net/jacobgoh101/fdvnsg6u/1/

According to the documentation, the full response is available as a response property on the error. So I'd use that information in the catch block: (async() => { let apiRes = null; try { apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a'); } catch (err) { console.error("Error response:"); console.error(err.response.data); // *** console.error(err.response.status); // *** console.error(err.response.headers); // *** } finally { console.log(apiRes); } })(); Updated Fiddle But if you want it in finally instead, just save it to a variable you can use there: (async() => { let apiRes = null; try { apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a'); } catch (err) { apiRes = err.response; } finally { console.log(apiRes); // Could be success or error } })();

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/05a2d8b5406ce9a42093d6e53e5eb7811e633326b1664cf6b3957abc5529a0d1
hash \u00b7 05a2d8b5406ce9a42093d6e53e5eb7811e633326b1664cf6b3957abc5529a0d1
Axios. How to get error response even when api return 404 er… — DepScope fix | DepScope