How to handle errors with Axios request
Overview
We can add the catch function to the axios request to handle the error response for the sent request.
The catch function will be called for status codes outside the range of 2xx.
Read this Answer to learn more about
axios.
Example
Let's write an example to handle errors for the request using the axios.
Explanation
In the code above,
Line 5: Include the
axioslibrary.Line 8: Use the
getmethod of theaxiosobject to make a get request. The URL provided to the get method points to a wrong endpoint so we will get the error for the request.Line 12: Add the
catchfor theaxiosrequest. For thecatchwe pass a callback function as an argument. The callback function will be triggered for status codes that fall outside the range of2xx.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved