在unhandledRejection上发送500

时间:2016-03-19 18:41:59

标签: javascript node.js express

如何在unhandledRejection上发送500错误

process.on("unhandledRejection", function(promise, reason){
   // deal with the rejection here.
   console.log('global error handler');
   console.log(reason);
});


// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.json({"error": err.message});
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({"error": err.message});
});

0 个答案:

没有答案