来自版本7的Node.js具有异步/等待语法糖来处理承诺,现在在我的代码中,以下警告经常出现:
(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.
不幸的是,这里没有引用到缺少catch的那一行。有没有办法不检查每个try/catch块就能找到它?
发布于 2017-05-16 15:27:00
监听进程的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
});
发布于 2019-03-16 09:35:46
使用堆栈跟踪进行日志记录
如果您正在寻找更多有用的错误消息。尝试将其添加到您的节点文件中。它应该显示发生崩溃的完整堆栈跟踪。
process.on('unhandledRejection', (error, p) => {
console.log('=== UNHANDLED REJECTION ===');
console.dir(error.stack);
});
https://stackoverflow.com/questions/43834559
复制相似问题