newerror

1.0.0 • Public • Published

newerror Travis build

Create Error subclasses without sweating it!

How?

var newError = require('newerror');
 
var StuffError = newError('StuffError');
 
// [...]
try {
    throw new StuffError();
} catch (e) {
    console.log(instanceof StuffError); // true
}

Why?

Creating a new Error subclass is quite easy, but there are some things you shouldn't forget:

  • The stack has to be re-created or the stack trace is lost.
  • The prototype has to be correctly set.
  • The name property has to be correctly set.

So this module is just to ease the creation of Error subclasses, and never get it wrong.

API

The newError function takes 2 arguments:

  • type: the name of the new Error subclass.
  • message: the default message when an error is thrown.

To use this function:

var newError = require('newerror');
 
var StuffError = newError('StuffError', 'test');
throw new StuffError(); // "StuffError: test"
throw new StuffError('another test'); // "StuffError: another test"

Contributors

License

MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i newerror

Weekly Downloads

0

Version

1.0.0

License

none

Last publish

Collaborators

  • ralt