【发布时间】:2019-04-26 19:54:50
【问题描述】:
我已经使用 NodeJS 和 Express 构建了一些登录系统。我的错误处理基于发送适当的 http 状态代码,旁边自定义错误消息。例如,不正确的登录会返回:
res.status(401).send({ error: 'Incorrect login' })
如您所见,我既设置了一个会导致 promise 失败的状态(至少在 Axios 中),并且发送了我自己的错误。
在前端,这就是我所拥有的:
try {
var response = await axios({
method: 'post',
url: 'http://localhost:3000/signin',
data: {
email: this.state.email,
password: this.state.password
},
headers
})
} catch (error) {
console.log(response)
return alert(error)
}
由于 401 导致 promise 被拒绝,我留下了这个内置错误:Request failed with status code 401。
还有什么方法可以访问响应对象吗?我的想法是处理来自服务器的所有错误消息,让这项工作远离前台。
【问题讨论】:
-
控制台记录错误对象?
-
错误对象不包含响应
标签: javascript http axios