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
.
Let's write an example to handle errors for the request using the axios
.
In the code above,
Line 5: Include the axios
library.
Line 8: Use the get
method of the axios
object 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 catch
for the axios
request. For the catch
we pass a callback function as an argument. The callback function will be triggered for status codes that fall outside the range of 2xx
.