Name: custom-verror
Owner: Particle
Description: Simple class extending VError for easy custom error creation
Created: 2017-10-04 12:50:03.0
Updated: 2018-03-18 03:51:56.0
Pushed: 2018-01-03 18:22:10.0
Homepage: null
Size: 5
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Simple class extending VError for easy custom error creation.
Here is the basic usage. For more use cases, see the tests.
UserNotAllowedError.js
strict';
t CustomVError = require('custom-verror');
eplace UserNotAllowedError with your custom error name
le.exports = class UserNotAllowedError extends CustomVError {
constructor(...args) {
super(...args);
// Provide a default message
this.message = this.message || 'User is not allowed to perform this action';
}
t UserNotAllowedError = require('./UserNotAllowedError');
tion performAction() {
if (!isAllowed) {
throw new UserNotAllowedError({
// You can provide some error metadata. This will be appended to the message
info: {
user_id: user._id
}
})
}
{
performAction();
tch (ex) {
if (ex instanceof UserNotAllowedError) {
numberOfTimesUsersTriedToDoSomethingTheyWerentSupposedTo++;
// This will log the default message and the metadata in following format:
// "User is not allowed to perform this action user_id=FOO"
console.log(ex);
}