Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

defekt

Package Overview
Dependencies
Maintainers
5
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

defekt

defekt is custom errors made simple.

  • 5.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
decreased by-8.43%
Maintainers
5
Weekly downloads
 
Created
Source

defekt

defekt is custom errors made simple.

defekt

Status

CategoryStatus
Versionnpm
DependenciesDavid
Dev dependenciesDavid
BuildCircleCI
LicenseGitHub

Installation

$ npm install defekt

Quick start

First you need to add a reference to defekt in your application:

const { defekt } = require('defekt');

If you use TypeScript, use the following code instead:

import { defekt } from 'defekt';

Then call the defekt function and hand over an object of custom error configurations that you would like to have created. In this object each key is the name for an error and the corresponding value is the error's configuration.

const errors = defekt({
  ArgumentNull: {},
  InvalidOperation: {},
  // ...
});

The result is an object containing all the errors you specified. To use one of those errors, simply call the appropriate function with new and throw it:

throw new errors.InvalidOperation();

By default, the error name is also used as the error message, but converted to a human readable form. E.g., the error name InvalidOperation becomes the message Invalid operation.:

try {
  throw new errors.InvalidOperation();
} catch (ex) {
  console.log(ex.message);
  // => 'Invalid operation.'
}

If you want to specify a custom message, feel free to do so:

throw new errors.InvalidOperation('Something failed.');

Additionally, if an error was caused by another error, you can specify this error using the cause property:

throw new errors.InvalidOperation('Something failed.', {
  cause: new Error(...)
});

From time to time, you may need to provide additional data for an error. For this, you can use the data property:

throw new errors.InvalidOperation('Something failed.', {
  data: { ... }
});

The custom errors follow the same rules as the built-in ones, i.e. they have a name and a message property, they derive from Error and they can be recognized by the util.isError function.

Defining error codes

By default, each custom error uses its uppercased name with an E prefix as error code. E.g., an InvalidOperation error uses EINVALIDOPERATION as its code.

From time to time, you may want to provide custom error codes. For that specify a code property on the configuration object:

const errors = defekt({
  ArgumentNull: { code: 'ARGNULL' },
  InvalidOperation: { code: 'INVALOP' }
  // ...
});

Running the build

To build this module use roboter:

$ npx roboter

Keywords

FAQs

Package last updated on 15 Nov 2019

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc