如何找到 Node.js UnhandledPromiseRejectionWarning 中未处理的承诺?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43834559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find which promises are unhandled in Node.js UnhandledPromiseRejectionWarning?
提问by user1658162
Node.js from version 7 has async/await syntactic sugar for handling promises and now in my code the following warning comes up quite often:
版本 7 的 Node.js 具有用于处理承诺的 async/await 语法糖,现在在我的代码中经常出现以下警告:
(node:11057) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): ReferenceError: Error: Can't set headers
after they are sent.
(node:11057) DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.
Unfortunately there's no reference to the line where the catch is missing. Is there any way to find it without checking every try/catch block?
不幸的是,没有提到丢失渔获物的那条线。有没有办法在不检查每个 try/catch 块的情况下找到它?
回答by cuixiping
listen unhandledRejectionevent of process.
监听unhandledRejection进程事件。
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
// application specific logging, throwing an error, or other logic here
});
回答by Sven Slootweg
The correctway to show a full stacktrace for unhandled ES6 Promise rejections, is to run Node.js with the --trace-warningsflag. This will show the full stacktrace for every warning, without having to intercept the rejection from within your own code. For example:
显示未处理的 ES6 Promise 拒绝的完整堆栈跟踪的正确方法是运行带有--trace-warnings标志的Node.js。这将显示每个警告的完整堆栈跟踪,而不必从您自己的代码中拦截拒绝。例如:
node --trace-warnings app.js
Ensure that the trace-warningsflag comes beforethe name of your .jsfile! Otherwise, the flag will be interpreted as an argument to your script, and it will be ignored by Node.js itself.
确保trace-warnings标志出现在您的.js文件名之前!否则,该标志将被解释为脚本的参数,Node.js 本身将忽略它。
If you want to actually handleunhandled rejections (eg. by logging them), then you might want to use my unhandled-rejectionmodule instead, which catches all the unhandled rejections for every major Promises implementation that supports it, with a single event handler.
如果您想实际处理未处理的拒绝(例如,通过记录它们),那么您可能需要使用我的unhandled-rejection模块,它使用单个事件处理程序捕获支持它的每个主要 Promise 实现的所有未处理的拒绝。
That module supports Bluebird, ES6 Promises, Q, WhenJS, es6-promise, then/promise, and anything that conforms to any of the unhandled rejection specifications (full details in the documentation).
该模块支持蓝鸟,ES6承诺,Q,WhenJS, ,es6-promise,then/promise和任何符合任何未处理的抑制规范(文档中的全部细节)的。
回答by joshuakcockrell
Logging with stack trace
使用堆栈跟踪记录日志
If you are looking for more of a helpful error message. Try adding this to your node file. It should display the full stack trace where your crash is happening.
如果您正在寻找更多有用的错误消息。尝试将其添加到您的节点文件中。它应该显示发生崩溃的完整堆栈跟踪。
process.on('unhandledRejection', (error, p) => {
console.log('=== UNHANDLED REJECTION ===');
console.dir(error.stack);
});

