Bluebird捕获错误的日志记录语法?

时间:2014-07-12 14:39:40

标签: node.js error-handling mongoose promise bluebird

与此相同的Bluebird承诺错误是什么:

    if (err) {
        console.log(err);
        res.send(err);
    }

为此:

}).catch(Promise.OperationalError, function(e){
    // handle error in Mongoose save findOne etc, res.send(...)
}).catch(function(e){
    // handle other exceptions here, this is most likely
    // a 500 error where the top one is a 4XX, but pay close
    // attention to how you handle errors here
});

1 个答案:

答案 0 :(得分:2)

Bluebird会自动为您记录未处理的拒绝。如果您希望针对错误发送服务器响应并将链标记为已处理,则可以执行以下操作:

.catch(function(err){
     console.log(err);
     res.send(err);
     throw err; // this is optional, if you don't want to mark the chain as handled.
 });