我正在尝试使用 Bluebird.js 的自定义错误处理程序。
在下面的示例中调用了包罗万象的处理程序,而不是 MyCustomError 处理程序,但是当我将拒绝移动到 then 函数(并解决了 firstPromise...)时,MyCustomError 处理程序叫做。 这是为什么?有什么问题吗?谢谢。
var Promise = require('bluebird'),
debug = require('debug')('main');
firstPromise()
.then(function (value) {
debug(value);
})
.catch(MyCustomError, function (err) {
debug('from MyCustomError catch: ' + err.message);
})
.catch(function (err) {
debug('From catch all: ' + err.message);
});
/*
* Promise returning function.
* */
function firstPromise() {
return new Promise(function (resolve, reject) {
reject(new MyCustomError('error from firstPromise'));
});
}
/*
* Custom Error
* */
function MyCustomError(message) {
this.message = message;
this.name = "MyCustomError";
Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype = Object.create(Error.prototype);
MyCustomError.prototype.constructor = MyCustomError;
最佳答案
在任何其他事情之前声明错误类,它将起作用(原型(prototype)分配不会被提升)
关于javascript - Bluebird.js 自定义错误捕获功能,不适用于第一个 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756903/