好吧,我可能只是错过了显而易见的东西,但我似乎找不到这个问题的一般答案,而且我的Google-Fu到目前为止让我失望了。
在 Promise 的 Catch 处理程序中,如何重新引发错误,同时仍保留原始错误的 Promise 堆栈跟踪?
这可能不是正确的描述,所以这里有一个例子:
https://jsfiddle.net/8sgj8x4L/19/
使用此代码,跟踪的堆栈为:
Warning: a promise was rejected with a non-error: [object String]
at RejectWithAnError (https://fiddle.jshell.net/_display/:51:19)
at https://fiddle.jshell.net/_display/:57:14
From previous event:
at StartTheProgram (https://fiddle.jshell.net/_display/:56:6)
at window.onload (https://fiddle.jshell.net/_display/:67:1)
bluebird.js:1444 Unhandled rejection an error occurred
但是,如果添加了 catch 处理程序,并且错误被重新拒绝或从该处理程序重新引发,则堆栈将成为新的 Reject 方法调用的位置:
https://jsfiddle.net/8sgj8x4L/18/
哪个跟踪此堆栈跟踪:
Warning: a promise was rejected with a non-error: [object String]
at https://fiddle.jshell.net/_display/:65:23
From previous event:
at StartTheProgram (https://fiddle.jshell.net/_display/:61:11)
at window.onload (https://fiddle.jshell.net/_display/:70:1)
bluebird.js:1444 Unhandled rejection an error occurred
您可以看到调度原始错误的内部方法"RejectWithAnError"从第二个堆栈中消失,因为错误被捕获并重新抛出。
作为参考,以下是来自JSFiddle的完整代码(最新的Bluebird作为外部依赖项引用(:
window.P.longStackTraces();
function RejectWithAnError() {
var err = {error: true, message: "an error occurred"};
err.prototype = new Error();
return window.P.reject(err);
}
function StartTheProgram() {
return RejectWithAnError()
// Comment out this catch handler completely, and the Promise stack trace will correctly show the "RejectWithAnError" method as the error origin.
.catch(function (status) {
console.log("[WARN] Catch handler was called.");
// Neither of these lines will show "RejectWithAnError" in the Promise chain stack trace.
// throw status;
return window.P.reject(status);
});
}
StartTheProgram()
(附带说明一下,这是我的第一个堆栈溢出问题,所以我希望这是这个问题的正确格式。
编辑:更新了示例以拒绝使用从新Error
实例继承的对象实例。
JavaScript 中的错误在创建时会捕获它们的跟踪,因此每当您重新抛出错误时,它都会自动捕获堆栈跟踪。
但是,您不会抛出错误。出于这个原因,蓝鸟给你一个警告(是的,我们(。如果您坚持抛出非错误而不是正确子类化错误 - 则需要通过手动捕获对象来手动欺骗对象以获得正确的堆栈跟踪。通过在构造函数中创建new Error
并将.stack
设置为其堆栈(可能需要执行一些分析(或调用特定方法:
function RejectWithAnError() {
var err = {error: true, message: "an error occurred"};
// err.__proto__ = new Error(); -- this is how you'd do it with prototypes btw
// explicitly capture the stack trace of the object
if(Error.captureStackTrace) Error.captureStackTrace(err);
}
在这里摆弄。请注意,.prototype
是函数用来指示通过将函数作为构造函数调用而创建的对象原型的属性。为了直接设置对象的原型,你可以调用__proto__
尽管这很少是一个特别好的主意。下面是一个使用 __proto__
而不是 Error.captureStackTrace 的示例。
Promise.config({
warnings: false
});