Custom VError
Simple class extending VError for easy custom error creation.
Usage
Here is the basic usage. For more use cases, see the tests.
1. Define your error
UserNotAllowedError.js
'use strict';
const CustomVError = require('custom-verror');
module.exports = class UserNotAllowedError extends CustomVError {
constructor(...args) {
super(...args);
this.message = this.message || 'User is not allowed to perform this action';
}
};
2. Throw your error
const UserNotAllowedError = require('./UserNotAllowedError');
function performAction() {
if (!isAllowed) {
throw new UserNotAllowedError({
info: {
user_id: user._id
}
})
}
}
3. Catch your error
try {
performAction();
} catch (ex) {
if (ex instanceof UserNotAllowedError) {
numberOfTimesUsersTriedToDoSomethingTheyWerentSupposedTo++;
console.log(ex);
}
}