I am trying to throw and then catch a custom error in a Bluebird promise chain, but I can't get it to catch the custom error. For example:
function login(req, res, next) {
function LoginError() {}
return User.where('id', req.body.userId).fetch()
.then(function (location) {
if (req.body.password !== location.get('password')) {
throw new LoginError();
}
// returns a promise
return Subscription.where('userId', location.get('userId')).fetch();
})
.then(function (subscription) {
return res.send(JSON.stringify(subscription));
})
.catch(LoginError, function (err) {
return res.send('Login error');
})
.catch(function (err) {
res.send('Other error: ' + JSON.stringify(err));
});
}
When the password doesn't match and it throws LoginError
, the error is caught in the second catch block, not the catch block for LoginError
. What am I doing wrong?
I'm using Express.js, Bluebird, and Bookshelf/Knex where User
is a Bookshelf model.