handle erros axios try catch code example
Example 1: how to handle error axios js
axios.post("/api/end", {data : "xx"})
.then(({ data }) => {
})
.catch((err) => {
let message = typeof err.response !== "undefined" ? err.response.data.message : err.message;
console.warn("error", message);
});
Example 2: axios call error handling
axios.get('/api/xyz/abcd')
.catch(function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
});