You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varerrorHandle=function(err,req,res,next){consterrorDetails=err.stack||err;res.status(err.status||500).format({json(){consterrorInfo={details: err,error: err.toString(),};res.send(errorInfo);},html(){constmessage=`<pre>${errorDetails}</pre>`;res.send(`<h1>500 Internal server error</h1>\n${message}`);},default(){constmessage=errorDetails;res.send(`500 Internal server error:\n${message}`);}});}module.exports=errorHandle;
比如运行第17个案例,
在登录注册时故意输错
点击登录浏览器会报以下错误:
而我们在处理api时,其实已经给出友好的错误的提示:比如用户不存在,密码有误等信息。而上面的错误不是我们想要的样子。
虽然使用了中间件errorHandel,但最终的错误处理是在app.js中处理的。
修改app.js 中错误处理的代码,比如新增自己对错误的处理,还是不行。甚至直接把render改为send都不奏效。
所以,该如何解决这个问题呢?
此外:
既然我们使用errorHandel去处理错误,为何不直接把res也传进来,遇到错误,直接返回响应?而不像案例中那样继续next将错误留给app中的处理错误的中间件处理。
The text was updated successfully, but these errors were encountered: