项目作者: jessety

项目描述 :
A modern take on JavaScript errors
高级语言: JavaScript
项目地址: git://github.com/jessety/modern-error.git
创建时间: 2020-01-02T02:44:02Z
项目社区:https://github.com/jessety/modern-error

开源协议:MIT License

下载


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

  1. npm install modern-error

Usage

  1. // Modules
  2. import ModernError from 'modern-error';
  3. // CommonJS
  4. const ModernError = require('modern-error');

Create an error with a message

  1. throw new ModernError('An error occurred');

Create an error with a message and additional properties

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

Create an error with an object

  1. throw new ModernError({
  2. message: 'An error occurred',
  3. code: 'D12',
  4. test: true
  5. });

Create an error by wrapping a native Error object

  1. const nativeError = new Error('An error occurred');
  2. nativeError.code = 'D12';
  3. throw new ModernError(nativeError);
  4. // The 'message' and 'code' properties are inherited from the native error object

Subclassing

modern-error is designed to be easily extensible.

  1. class CustomError extends ModernError {}
  2. const error = new CustomError();
  3. console.log(error instanceof CustomError); // true

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

  1. class ErrorWithDefaults extends ModernError() {
  2. static get defaults() {
  3. return {
  4. test: false,
  5. code: null,
  6. details: 'No further details'
  7. };
  8. }
  9. }
  10. const error = new ErrorWithDefaults('Test', { code: 'D12' });
  11. // error's properties are:
  12. // message: 'Test'
  13. // test: false
  14. // code: 'D12'
  15. // details: 'No further details'

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

  1. const HTTPError = ModernError.subclass({
  2. name: 'HTTPError',
  3. defaults: { status: 500 },
  4. serialize: ['message', 'stack']
  5. });
  6. const error = new HTTPError('Error occurred');
  7. // error's properties are:
  8. // message: Error occurred
  9. // status: 500

Defaults

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

  1. ModernError.defaults = {
  2. status: 500,
  3. test: false
  4. };

Or overriding the defaults static function of a subclass:

  1. class ErrorWithDefaults extends ModernError() {
  2. static get defaults() {
  3. return {
  4. status: 500,
  5. test: false
  6. };
  7. }
  8. }

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

  1. const ErrorWithDefaults = ModernError.subclass({
  2. defaults: {
  3. status: 500,
  4. test: false
  5. }
  6. });

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.

  1. ModernError.serialize = ['message', 'stack'];
  2. // From now on, all JSON representations of a ModernError will include the `stack` property

Or overriding the getter in a subclass.

  1. class StackError extends ModernError {
  2. static get serialize() {
  3. return ['message', 'stack'];
  4. }
  5. }
  6. const error = new StackError('An error has occurred');
  7. console.log(JSON.stringify(error));
  8. // JSON representation will include the `stack` property

All property keys are sorted alphabetically before serialization.

License

MIT © Jesse Youngblood