Create Error subclasses without sweating it!
var newError = require('newerror');
var StuffError = newError('StuffError');
// [...]
try {
throw new StuffError();
} catch (e) {
console.log(e instanceof StuffError); // true
}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
nameproperty has to be correctly set.
So this module is just to ease the creation of Error subclasses, and never get it wrong.
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"MIT License.
