Error Class
This module provides typed errors that closely emulate the native Error
class to a pedantic degree.
$ npm install error-class
Usage
The default export is a function that accepts two arguments, the name of the typed error, and an optional function which gets invoked in the context of the error instance with the error arguments after the error setup is completed.
import errorClass from 'error-class'
const SpecialError = errorClass('SpecialError', function () {
if (arguments.length > 1) this.specialProperty = arguments[1]
})
const instance = new SpecialError('baz', { foo: 'bar' })
instance.message
instance.specialProperty
Details
import errorClass from 'error-class'
const HumanError = errorClass('HumanError')
const hungryError = new HumanError(`I'm hungry!`)
hungryError.name
hungryError.message
hungryError.stack
hungryError.hasOwnProperty('name')
hungryError.hasOwnProperty('message')
hungryError.hasOwnProperty('stack')
const thirstyError = HumanError(`I'm thirsty!`)
Object.keys(thirstyError).length === 0
Object.keys(Object.getPrototypeOf(instance)).length === 0
thirstyError.constructor === HumanError
thirstyError.constructor.name === 'HumanError'
thirstyError instanceof Error
thirstyError instanceof HumanError
License
This software is licensed under the MIT License.