Socket
Book a DemoInstallSign in
Socket

custom-verror

Package Overview
Dependencies
Maintainers
6
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

custom-verror

Simple class extending VError for easy custom error creation

0.1.1
latest
Source
npmnpm
Version published
Weekly downloads
272
-34.14%
Maintainers
6
Weekly downloads
 
Created
Source

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

'use strict';

const CustomVError = require('custom-verror');

// Replace UserNotAllowedError with your custom error name
module.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

const UserNotAllowedError = require('./UserNotAllowedError');

function 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

try {
	performAction();
} catch (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);
	}
}

FAQs

Package last updated on 10 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.