I am currently experimenting with the custom error handlers in Bluebird.js.
In the code snippet below, I noticed that the catch-all handler gets called instead of the MyCustomError handler. However, when I moved the rejection inside the then function (and resolved the firstPromise...), the MyCustomError handler was triggered. Can anyone explain why this is happening? Did I make a mistake somewhere? Thank you.
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);
});
/*
* Function that returns a promise.
* */
function firstPromise() {
return new Promise(function (resolve, reject) {
reject(new MyCustomError('error from firstPromise'));
});
}
/*
* Custom Error Definition
* */
function MyCustomError(message) {
this.message = message;
this.name = "MyCustomError";
Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype = Object.create(Error.prototype);
MyCustomError.prototype.constructor = MyCustomError;