在Q中记录所有拒绝的承诺

时间:2022-08-23 11:41:12

Is there a way to configure Q to log or call a specific function on all rejected promises (like an interceptor)?

是否有一种方法可以将Q配置为对所有被拒绝的承诺(如拦截器)进行日志记录或调用特定函数?

Many exceptions are being swallowed in my application, and put error handling in all my promises just for logging purposes would be duplicated work to do.

在我的应用程序中包含了许多异常,并且将错误处理放在我的所有承诺中,只用于日志记录的目的将被重复使用。

Thanks!

谢谢!

1 个解决方案

#1


3  

Q actually already supports this - as of 1.3.0 Q offers the standard unhandled rejection hooks:

Q实际上已经支持了这一点——因为1.3.0 Q提供了标准的未处理的拒绝挂钩:

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

You can also log caught errors from .done with Q.onerror:

您还可以记录捕获的错误,从。完成Q.onerror:

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

#1


3  

Q actually already supports this - as of 1.3.0 Q offers the standard unhandled rejection hooks:

Q实际上已经支持了这一点——因为1.3.0 Q提供了标准的未处理的拒绝挂钩:

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

You can also log caught errors from .done with Q.onerror:

您还可以记录捕获的错误,从。完成Q.onerror:

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

相关文章