【问题标题】:Use process.on('uncaughtException to show a 500 error page使用 process.on('uncaughtException 显示 500 错误页面
【发布时间】:2012-04-19 03:36:30
【问题描述】:

我正在开发一个快递应用。

我的 server.js 目前有以下内容

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

这会在每次出现错误时阻止服务器崩溃,但是,它只是坐在那里......

是否可以代替 console.log 发送带有错误详细信息的 500 错误页面?

【问题讨论】:

    标签: node.js error-handling express


    【解决方案1】:

    我认为您不能在uncaughtException 中做出响应,因为即使没有请求发生也可能发生这种情况。

    Express 本身提供了一种在路由内handle errors 的方法,如下所示:

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

    【讨论】:

    【解决方案2】:

    根据ExpressJS Error Handling,在您的其他app.use 语句下方添加app.use(function(err, req, res, next){ // your logic });

    例子:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多