error-subclass
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/error-subclass package

2.2.0 • Public • Published

ErrorSubclass

NPM

Build Status

Dependencies DevDependencies

A JavaScript class that makes Error subclassable. It inherits from Error (in all the right ways), so that you can safely and easily create your own Error subclasses.

The provided class handles all the craziness for preserving stack traces and other things so that you don't have to worry about it. Create a class that inherits from it and you're good to go.

  • instanceof Error
  • instanceof <your subclass>
  • stack traces
  • .name === <name of subclass>
  • .toString()

Usage

import ErrorSubclass from 'error-subclass';
 
class MyError extends ErrorSubclass {
 
  static displayName = 'MyError'; // optional. survives minification
 
  // Add extra properties/methods if desired:
 
  get code() {
    return 'MYERROR';
  }
 
}
 
// later...
throw new MyError('error message');

License

MIT

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.2.0
    187
    • latest

Version History

Package Sidebar

Install

npm i error-subclass

Weekly Downloads

369

Version

2.2.0

License

MIT

Last publish

Collaborators

  • spudly