admin管理员组文章数量:1400286
I have a question about axios and error handling. So this is the method I'm using to handle errors when the user logins from the front end:
axios.post('http://localhost:3001/login',
{
login: user.login,
password: user.password,
}
)
.then(
(response) => {
// code
},
(error) => {
// error handling
}
);
This is the second method:
axios.post('http://localhost:3001/login',
{
login: user.login,
password: user.password,
}
)
.then(
(response) => {
// code
}
).catch((error) => {
// error handling
});
What is the best approach? Is it the same? When the server is unreachable the error message is the same: "Network Error". Is there any way to get a more detailed error? (For example in the console I get a CORS error)
I have a question about axios and error handling. So this is the method I'm using to handle errors when the user logins from the front end:
axios.post('http://localhost:3001/login',
{
login: user.login,
password: user.password,
}
)
.then(
(response) => {
// code
},
(error) => {
// error handling
}
);
This is the second method:
axios.post('http://localhost:3001/login',
{
login: user.login,
password: user.password,
}
)
.then(
(response) => {
// code
}
).catch((error) => {
// error handling
});
What is the best approach? Is it the same? When the server is unreachable the error message is the same: "Network Error". Is there any way to get a more detailed error? (For example in the console I get a CORS error)
Share Improve this question asked Oct 15, 2019 at 2:27 devamatdevamat 2,5139 gold badges33 silver badges57 bronze badges2 Answers
Reset to default 8The error can occur at different parts - Request, Response.
Request errors occur when there is no response. Like 404 etc, which has no default response.
Response errors have when API sends custom response to handle errors.
I used to handle this way:
const handleErrorResponse = (error) => {
let errorResponse;
if(error.response && error.response.data) {
// I expect the API to handle error responses in valid format
errorResponse = error.response.data;
// JSON stringify if you need the json and use it later
} else if(error.request) {
// TO Handle the default error response for Network failure or 404 etc.,
errorResponse = error.request.message || error.request.statusText;
} else {
errorResponse = error.message;
}
throw new Error(errorResponse);
}
now,
axios.get(/foo/bar)
.then(res => doSOmething())
.catch(err => handleErrorResponse(err))
The I use error handling the error response as string. The same you can use it with axios interceptor should you need.
This is not valid, then
accepts single argument
.then(
(response) => {
// code
},
(error) => {
// error handling
}
);
Only this's valid
.then(
(response) => {
// code
}
).catch((error) => {
// error handling
});
版权声明:本文标题:javascript - How to properly handle axios errors and how to get detailed error descriptions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744258420a2597593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论