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….

The post How to handle Axios error appeared first on CodeSource.io.


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to handle Axios error." Deven | Sciencx - Friday October 29, 2021, https://www.scien.cx/2021/10/29/how-to-handle-axios-error/
HARVARD
Deven | Sciencx Friday October 29, 2021 » How to handle Axios error., viewed ,<https://www.scien.cx/2021/10/29/how-to-handle-axios-error/>
VANCOUVER
Deven | Sciencx - » How to handle Axios error. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/29/how-to-handle-axios-error/
CHICAGO
" » How to handle Axios error." Deven | Sciencx - Accessed . https://www.scien.cx/2021/10/29/how-to-handle-axios-error/
IEEE
" » How to handle Axios error." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/10/29/how-to-handle-axios-error/. [Accessed: ]
rf:citation
» How to handle Axios error | Deven | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.