Use process.on('uncaughtException to show a 50

2019-03-28 14:13发布

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?

2条回答
叼着烟拽天下
2楼-- · 2019-03-28 14:29

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
});
查看更多
爷的心禁止访问
3楼-- · 2019-03-28 14:37

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
});
查看更多
登录 后发表回答