Socket
Socket
Sign inDemoInstall

modern-error

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    modern-error

A modern take on JavaScript errors


Version published
Weekly downloads
7
decreased by-58.82%
Maintainers
1
Install size
11.0 kB
Created
Weekly downloads
 

Readme

Source

modern-error

A modern take on JavaScript errors

ci coverage npm license

modern-error is a drop-in replacement for the native Error class, with slightly more modern features:

  • Create and throw errors with additional properties in one line
  • Instantiate errors with a string, object, or existing native error
  • Serialize the message property to JSON by default, without the need for a JSON replacer function or subclass
  • Customize which non-enumerable properties are serialized without subclassing
  • Easily define subclasses with default properties

Installation

npm install modern-error

Usage

// Modules
import ModernError from 'modern-error';

// CommonJS
const ModernError = require('modern-error');

Create an error with a message

throw new ModernError('An error occurred');

Create an error with a message and additional properties

throw new ModernError('An error occurred', { code: 'D12' });

Create an error with an object

throw new ModernError({
  message: 'An error occurred',
  code: 'D12',
  test: true
});

Create an error by wrapping a native Error object

const nativeError = new Error('An error occurred');
nativeError.code = 'D12';

throw new ModernError(nativeError);

// The 'message' and 'code' properties are inherited from the native error object

Subclassing

modern-error is designed to be easily extensible.

class CustomError extends ModernError {}

const error = new CustomError();

console.log(error instanceof CustomError); // true

Subclasses may also define custom properties by overriding the static 'defaults' getter

class ErrorWithDefaults extends ModernError() {

  static get defaults() {
    return {
      test: false,
      code: null,
      details: 'No further details'
    };
  }
}

const error = new ErrorWithDefaults('Test', { code: 'D12' });

// error's properties are:
// message: 'Test'
// test: false
// code: 'D12'
// details: 'No further details'

Subclasses may alternatively be created by invoking the static subclass function.

const HTTPError = ModernError.subclass({
  name: 'HTTPError',
  defaults: { status: 500 },
  serialize: ['message', 'stack']
});

const error = new HTTPError('Error occurred');

// error's properties are:
// message: Error occurred
// status: 500

Defaults

Defaults properties for instances of ModernError may be defined by setting the class defaults property:

ModernError.defaults = {
  status: 500,
  test: false
};

Or overriding the defaults static function of a subclass:

class ErrorWithDefaults extends ModernError() {
  static get defaults() {
    return {
      status: 500,
      test: false
    };
  }
}

Or, finally, by declaring defaults in a quick subclass:

const ErrorWithDefaults = ModernError.subclass({
  defaults: {
    status: 500,
    test: false
  }
});

Serialization

modern-error serializes its non-enumerable message property to JSON by default. You may also specify which other non-enumerable properties to serialize without subclassing by setting the class property serialize.

ModernError.serialize = ['message', 'stack'];

// From now on, all JSON representations of a ModernError will include the `stack` property

Or overriding the getter in a subclass.

class StackError extends ModernError {
  static get serialize() {
    return ['message', 'stack'];
  }
}

const error = new StackError('An error has occurred');

console.log(JSON.stringify(error));
// JSON representation will include the `stack` property

All property keys are sorted alphabetically before serialization.

License

MIT © Jesse Youngblood

Keywords

FAQs

Last updated on 31 May 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc