npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modern-error

v1.2.1

Published

A modern take on JavaScript errors

Downloads

51

Readme

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