在试图在nodejs上的asynchronous承诺链上使用未定义的variables时,在控制台或日志中没有uncaughtException

我注意到一些很奇怪的东西:

在asynchronous承诺链上,当我错误地尝试logging一个不存在的variables时,它创build了一个我在控制台中看不到的exception。

当我以同步的方式调用完全相同的函数(直接从方法中调用,而不是由于parsing的promise链asynchronous激活),它显示了预期的错误(请参阅下面的堆栈跟踪的例外情况)。

这使我疯狂,因为承诺链激活了方法(我可以看到它开始通过我开始添加的日志行来做事情),但是然后到达有问题的日志行,停止了整个方法实现其目标,但没有打印错误,也没有看到堆栈跟踪。

这使得整个debugging问题非常复杂,我相信我在这里做错了什么。 但是这是什么?

这是我使用的代码:(我标记写得不好的variables)

module.exports = { sync: function(){ console.log(doesNotExist); // error logs }, async: function(){ Q().then(function(){ // actually from an API returning a promise console.log(doesNotExist); // no error in log or `unhandledException` process event }); } }; 

更新:利用本杰明的解决scheme后

 process.on("unhandledRejection", function(r, e){ console.log("Oh No! Unhandled rejection!! \nr::", r,"\ne::", e); }); 

事情变得更好了。 现在我得到一些日志打印,看起来像这样

 Oh No! Unhandled rejection!! r:: [ReferenceError: Resolved123PasswordHash is not defined] e:: { state: 'rejected',reason: [ReferenceError: Resolved123PasswordHash is not defined] } 

但仍然没有像在同步方法调用一样的实际堆栈跟踪。

这是当我直接调用方法没有承诺链时的样子:

 2015-07-05T18:22:51.405Z :: [ error ] :: uncaughtException: Resolved123PasswordHash is not defined["ReferenceError: Resolved123PasswordHash is not defined", " at addUserAfterHashResolved (I:\\iBitch\\services\\userServices.js:19:77)", " at Object.module.exports.addUser (I:\\iBitch\\services\\userServices.js:22:3)", " at Object.<anonymous> (I:\\iBitch\\ikuter.js:263:14)", " at Module._compile (module.js:460:26)", " at Object.Module._extensions..js (module.js:478:10)", " at Module.load (module.js:355:32)", " at Function.Module._load (module.js:310:12)", " at Function.Module.runMain (module.js:501:10)", " at startup (node.js:129:16)", " at node.js:814:3"] 

即使在使用asynchronous承诺的情况下,是否可以获得完整的堆栈跟踪?

因为错误信息是好的,但它仍然没有指示我具体的代码行,所以我可以知道我应该修复。

还有一个编辑::我发现,如果我在承诺链的末尾使用“.done()”,它会显示堆栈跟踪,但它会崩溃整个应用程序(这也是不受欢迎的行为)也许这是方向我需要采取,但修改了一下? 我明白,如果我将开始把每个承诺链下try / catch它将严重影响性能,所以显然不是补充解决scheme在这里…

无论如何,任何进一步的帮助将appriciated。

假设你使用Q 1.3+,你正在寻找unhandledRejection而不是uncaughtException

 process.on("unhandledRejection", function(r, e){ console.log("Oh No! Unhandled rejection!!", r, e); }); 

在1.3之前,这个function在Q中不存在。像蓝鸟这样的其他库现在已经内置了这个function。

承诺以这种方式行事的原因是你将来可能会附加一个.catch处理程序,这将允许你处理错误。 stream程事件假设,如果你没有在事件循环中附加一个.catch ,你永远不会(合理的IMO)。