Get The Response Status From An Axios Error
You can make API requests with axios
. If the request fails because of a 4xx or 5xx response, an error will be raised. The response to the request is stored on the error so that you can still access the details of the response in your error handling logic.
Here is a snippet of code that details using async/await with axios
. It wraps the call in a try/catch
block.
In the case of the second call that results in a 404 response, the catch
block is executed. This pulls the response
off the error (e
).
Just like the standard response, the response from the error contains status
, data
, headers
, etc.
This also works with a promise-based control flow.
Last updated