I'm not experienced enough with Q. However, I can answer with bluebird.
bluebird supports typed catches. Which means you can create new Error subclasses, throw them, and handle them accordingly.
I've used newerror
in my example to simplify the creation of Error subclasses.
var newerror = require('newerror');
var DuplicateEmailError = newerror('DuplicateEmailError');
router.post('/user', function(req, res) {
var user = new User(req.body);
User.findOneAsync({ email: req.body.email }).then(function(existingUser) {
if (existingUser) {
throw new DuplicateEmailError('Error: User with email ' + existingUser.email + ' already exists.');
}
return User.createAsync(user);
}).then(function(createdUser) {
res.json({ id: createdUser.id }, 201);
}).catch(DuplicateEmailError, function(err) {
res.send(err.message, 409);
}).catch(function(err) {
res.send(err.message, 500);
});
});