javascript

How to handle Axios error


In this article you will learn about how to handle axios error.

In JavaScript, Axios is a promise-based API, which is basically a JavaScript library. It is mainly used to make HTTP requests from Node and is also used in front-end applications.

As Axios requests is a promise-based API, it lets you perform promise chaining by providing then() and catch() function. As a matter of fact, you can easily handle Axios error via the catch() function. See the code below:

const err = await axios.get('<https://test.org/status/404>').
  catch(err => err);

err.message; // 'Request failed with status code 404'

You can also use promise chaining in Axios error handling as it behaves exactly same as the promise catch(). See the example code below:

const err = await axios.get('<https://httpbin.org/status/200>').
  then(res => res.doesNotExist.throwAnError).
  catch(err => err);

here, you may add a catch() at the end to handle any errors that occur in the promise chain.

This is all about axios error and you may get an overview from it.


Share on social media

//