未捕获从 mongoose-promise 回调函数抛出的错误

Error thrown from a mongoose-promise callback function is not caught

我花了太多时间试图弄清楚为什么我的 express.js 控制器没有响应简单的查询,并发现 Mongoose-promise 回调引发的运行时错误正在悄悄地中断回调过程。

这是我的代码的简化版本:

server.get('/api/test', function (req, res, next) {
  User.find({}).exec().then(function success(users){
    console.log('SUCCESS');  
    typo[0] = 1; // throws a runtime error
    res.json(users);
  }, function error(err){
    console.log('ERROR');  
    res.json({error: err});
  });
});

这导致 SUCCESS 出现在我的控制台中,但随后什么也没有发生。没有给用户任何响应,我的错字导致的错误没有出现在我的控制台中,也没有调用错误回调。

我知道不应该从回调函数中抛出异常,但在那种情况下,这只是一个拼写错误,对我来说被警告是有意义的(例如,我的标准输出中的堆栈跟踪)每当有人犯这种错误时。 (毕竟我们是人类...)

在您看来,每当在 promise 回调中出现此类错误时,获得反馈的最佳方式是什么?

exec() 有两个承诺

.then(function) 
.then(null , function)

试试这个,我认为它会有所帮助

server.get('/api/test', function(req, res, next) {
    User.find({}).exec()
        .then(function success(users) {
            console.log('SUCCESS');
            typo[0] = 1; // throws a runtime error
            res.json(users);
        })
        .then(null, function error(err) {
            console.log('ERROR');
            res.json({
                error: err
            });
        });
});

这是 Mongoose 的错,因为它使用了错误的承诺实现。 Promise 是抛出安全的,因此异常会被捕获(因此它们可以在以后由未来的代码处理)——未来的代码永远不会到来,Mongoose 也不会报告它没有发生。良好的承诺实施不会遇到这个问题。

你有两个选择:

使用像 Bluebird 这样的库:

var Promise = require("bluebird");
var mongoose = Promise.promisifyAll(require("mongoose"));

User.findAsync({}).then(function(data){
    JSON.prase("dsa"); // not a silent failure, will show up, easy debugging
});

这具有比猫鼬承诺更快的优势,因此没有性能损失。或者,如果您非常保守并且不想要 bluebird 的性能和 API 收益 - 您可以使用本机承诺:

// Promise is the native promise
Promise.resolve(User.find({}).exec()).then(function(data){
    JSON.prase("dsa");
});

然后,假设您是 运行 nodejs 的现代变体(阅读:io.js v 1.4.1 或更高版本),您可以订阅承诺拒绝:

process.on("unhandledRejection", function(p, why){
    console.log("FOUND ERROR!!!!", p , why);
});

所以异常不会被静默抑制。