I've read the following questions but I couldn't find the solution.
- https://github.com/axios/axios/issues/960
- https://github.com/axios/axios/issues/41
- axios catch doesn't catch error
I wanna to get errors 400, 500, ... someone has said you should catch in then but I tried it and nothing happened. for test I wrote a function to get error 404, 400, ...
export const myAxios = url => {
Axios.get(url)
.then(response => {
// Success
console.log("then response ", response);
})
.catch(error => {
// Error
if (error.response) {
console.log("error respone", error.response);
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
// console.log(error.response.data);
// console.log(error.response.status);
// console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the
// browser and an instance of
// http.ClientRequest in node.js
console.log("erros request: ", error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error message", error.message);
}
console.log("errors config : ", error.config);
});
};
axios doesn't catch errors and browser console logs this:
xhr.js:172 GET http://localhost:3000/app/..../choices 404 (Not Found)
dispatchXhrRequest @ xhr.js:172
xhrAdapter @ xhr.js:11
dispatchRequest @ dispatchRequest.js:59
Promise.then (async)
request @ Axios.js:53
Axios.<computed> @ Axios.js:68
wrap @ bind.js:9
myAxios @ textaxios.js:4
(anonymous) @ List.jsx:14
commitHookEffectList @ react-dom.development.js:22030
Any tips or help will be appreciated.
packages:
"axios": "^0.19.0"
"react": "^16.12.0"