This content originally appeared on CodeSource.io and was authored by Deven
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.
The post How to handle Axios error appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-10-29T16:16:48+00:00) How to handle Axios error. Retrieved from https://www.scien.cx/2021/10/29/how-to-handle-axios-error/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.