A good option is to use the constructor property of the object:
// on reject / failure
switch (error.constructor) {
case NotAllowedError:
return res.send(400);
case DatabaseEntryNotFoundError:
return res.send(404);
default:
log('Failed to do something async with an unspecified error: ', error);
return res.send(500);
}
Notice that the constructor must match exactly with the one that object was created (suppose error is an instance of NotAllowedError and NotAllowedError is a subclass of Error):
error.constructor === NotAllowedErroristrueerror.constructor === Errorisfalse
This makes a difference from instanceof, which can match also the super class:
error instanceof NotAllowedErroristrueerror instanceof Erroristrue
Check this interesting post about constructor property.