Use process.on('uncaughtException to show a 500 error page

≯℡__Kan透↙ 提交于 2020-01-01 05:23:19

问题


I'm developing an express app.

I currently have the following in my server.js

process.on('uncaughtException', function (err) {
    console.log( "UNCAUGHT EXCEPTION " );
    console.log( "[Inside 'uncaughtException' event] " + err.stack || err.message );
});

Which stops the server crashing every time there's an error, however, it just sits there...

is it possible to instead of console.log, to send a 500 error page with the error details?


回答1:


I don't think you can from within the uncaughtException do a response since that could happen even when there is no request occurring.

Express itself provides a way to handle errors within routes, like so:

app.error(function(err, req, res, next){
    //check error information and respond accordingly
});



回答2:


Per ExpressJS Error Handling, add app.use(function(err, req, res, next){ // your logic }); below your other app.use statements.

Example:

app.use(function(err, req, res, next){
  console.log(err.stack);
  // additional logic, like emailing OPS staff w/ stack trace
});


来源:https://stackoverflow.com/questions/10221309/use-process-onuncaughtexception-to-show-a-500-error-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!