npm package that simplifies inheriting from Error
.
const UserError = require('@maxtruxa/user-error');
throw new UserError('Hello, World!', {additional: 'information'});
npm install @maxtruxa/user-error
- Subclasses
Error
without breaking standard behavior. - Like
Error
,UserError
takes a message as first argument. - Add properties by simply passing in an object as second or first argument
- Properties are enumerable.
That means serialization using
JSON.stringify
works as expected.
Just require
the package to get UserError
and either build your own error class on top of it or use it directly.
const UserError = require('@maxtruxa/user-error');
throw new UserError('oops', {additional: 'information'});
const UserError = require('@maxtruxa/user-error');
// Custom error class that takes an additional "error id" as first argument.
function MyError(id, message, properties) {
UserError.call(this, message, properties);
this.id = id;
}
MyError.prototype = Object.create(UserError.prototype, {
constructor: {value: MyError, configurable: true, writable: true}
});
throw new MyError('foo_error', 'oops');
When nesting exceptions, the inner exception should be called inner
by convention:
try {
throw new UserError('oops');
} catch (err) {
// "MyError" from the inheritance example above.
throw new MyError('internal_error', 'something failed', {inner: err});
}
Create a new error with the specified message and properties.
The resulting object has the following properties:
name
message
stack
- additional properties (copied from the
properties
argument)
name
is taken from (in that order):
properties.name
(useful for overriding the name without inheriting)this.constructor.prototype.name
(useful for minified code)this.constructor.name
message
is taken from (in that order):
properties.message
message
stack
is taken from (in that order):
properties.stack
- generated using
Error.captureStackTrace
(if available)
anything (default = ''
)
The message
property of the error. The value is always converted to a string.
new UserError(); // default
new UserError('test');
object (default = {}
)
Additional properties to be assigned to the error.
new UserError(); // default
new UserError({foo: 'bar'}); // without message
new UserError('test', {baz: 'qux'}); // with message
To run the test suite, install dependencies, then run npm test
:
npm install
npm test
Coverage reports are generated by running npm run coverage
.
Linting is done with npm run lint
.