Create custom errors that inherit Error.
Extending Error is a real pain. This library deals with the quirks, providing a clean API to extend Error that works across JS environments, including Node and browsers.
Returns a new subclass of Error, or of ParentError if it is provided.
Required
Type: string
The display name of this class's errors. For example, the builtin TypeError class's name is "TypeError"
. This affects how the error is displayed when it is thrown.
Type: Error
, Error
descendant
Default: Error
The Error type to be subclassed.
var customError = require('custom-error');
var ApocalypseError = customError('ApocalypseError');
ApocalypseError() instanceof Error // true
ApocalypseError() instanceof ApocalypseError // true
var UnixApocalypseError = customError('UnixApocalypseError', ApocalypseError)
UnixApocalypseError() instanceof Error // true
UnixApocalypseError() instanceof ApocalypseError // true
UnixApocalypseError() instanceof UnixApocalypseError // true
if (new Date().getFullYear() === 2038) {
throw UnixApocalypseError('OH NOES')
}
UnixApocalypseError.prototype.year = 2038
try {
throw UnixApocalypseError()
}
catch (err) {
console.log(err.year) // 2038
}
npm install custom-error
MIT