particle-iot/custom-verror

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Custom VError

Build Status

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

 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';
}

2. Throw your error
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
        }
    })
}

3. Catch your error
{
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);
}


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.