Skip to content
Advertisement

How to get catch error 404 error in Axios?

I have this snippet of code (part of a function), notice the “BadURL” at the end of the URL:

import axios from “axios”;

JavaScript

But canno’t get into the catch block, says:

JavaScript

I can catch the error only if i wrap the function call itself outside the class

Advertisement

Answer

Axios.post(...) is an asynchronous call that returns a promise, that statement doesn’t fail, and even if it does, it’s not because of the HTTP request failing.

What you need to use is the .then() and .catch() methods of the returned promise to handle the request.

JavaScript

Another alternative is to use async await.

JavaScript
Advertisement