I'm sending a POST request with Axios to my RESTful API which returns HTTP 422 in certain cases. I expect Axios to catch those errors in 'catch' block, the code enters in that but a javascript error is logged as well in browser console.
My sample request is like below. It enters the first "if" (if (error.response)
) as expected but also creates a javascript error which breaks the rest of the code.
this.Axios.post('<api_url>', data)
.then(function (response) {
console.log(response);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
self.setState((prevState) => {
return {
form_state: {
error: true,
message: error.response.data
}
};
});
} else if (error.request) {
self.setState((prevState) => {
return {
form_state: {
error: true,
message: error.request
}
};
});
} else {
self.setState((prevState) => {
return {
form_state: {
error: true,
message: error.message
}
};
});
}
console.log(error.config);
});
Am I missing something?
Thanks