use*_*162 162 warnings unhandled-exception node.js promise async-await
来自版本7的Node.js具有async/await语法糖用于处理promises,现在在我的代码中经常出现以下警告:
(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.
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有提到缺少捕获的线.有没有办法找到它而不检查每个try/catch块?
cui*_*ing 275
听取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
});
Run Code Online (Sandbox Code Playgroud)
Sve*_*weg 67
显示未处理的ES6 Promise拒绝的完整堆栈跟踪的正确方法是使用--trace-warnings
标志运行Node.js.这将显示每个警告的完整堆栈跟踪,而不必从您自己的代码中拦截拒绝.例如:
node --trace-warnings app.js
确保trace-warnings
标志位于文件名之前.js
!否则,该标志将被解释为您的脚本的参数,Node.js本身将忽略该标志.
如果要真正地处理未处理的拒绝(例如,通过记录他们),那么你可能想用我的unhandled-rejection
模块来代替,该捕获所有支持它,用一个单一的事件处理程序每一个主要的承诺执行未处理的拒绝.
该模块支持蓝鸟,ES6承诺,Q,WhenJS, ,es6-promise
,then/promise
和任何符合任何未处理的抑制规范(文档中的全部细节)的.
ege*_*ter 12
这个模块让我能够追踪罪魁祸首的承诺:https: //www.npmjs.com/package/trace-unhandled
安装
npm i trace-unhandled
Run Code Online (Sandbox Code Playgroud)
包含在代码中
require('trace-unhandled/register');
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找更多有用的错误消息。尝试将其添加到您的节点文件中。它应该显示发生崩溃的完整堆栈跟踪。
process.on('unhandledRejection', (error, p) => {
console.log('=== UNHANDLED REJECTION ===');
console.dir(error.stack);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
53072 次 |
最近记录: |