【问题标题】:Log all reject promises in Q在 Q 中记录所有拒绝承诺
【发布时间】:2015-06-15 05:59:50
【问题描述】:

有没有办法配置 Q 来记录或调用所有被拒绝的 Promise 的特定函数(如拦截器)?

在我的应用程序中吞下了许多异常,并且将错误处理放在我的所有承诺中仅用于记录目的将是重复的工作。

谢谢!

【问题讨论】:

  • 自 V1.3 起 unhandled rejection tracking 在 Q 中可用
  • @Bergi 我想 OP 想要tap 所有的拒绝,我猜。那是不可用的,对吧?
  • @thefourtheye:我知道他只是想拦截那些被吞下的。

标签: node.js promise q


【解决方案1】:

Q 实际上已经支持这一点 - 从 1.3.0 开始,Q 提供 standard unhandled rejection hooks

process.on("unhandledRejection", function(reason, p) {
  console.log("Unhandled rejection detected ", reason, p);
});

您也可以登录caught errors from .done with Q.onerror:

Q.onerror = function(error){
    // errors will be here and not thrown in `done` chains.
};

【讨论】:

  • Q.onerror 也仅用于未捕获的错误,对吧?
  • @thefourtheye 是的,但对于 handled 未捕获的 - 即 - 你通过写 .done() 隐式捕获的。
  • 这正是我想要的。有没有办法打印错误堆栈跟踪(如果有)?
  • @RicardoMayerhofer 是的,reason 参数是您拒绝的错误,您可以打印它的堆栈跟踪 - 请务必在您的顶部启用长堆栈跟踪 (Q.longStackTraces = true)代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 2016-10-20
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
相关资源
最近更新 更多