ErrorEx
'Extensible Error Class' implementation and predefined additional error types for javascript.
Installation
$ npm install errorex --save
Usage
var {ErrorEx, RangeError} = require('errorex');
var error = new erx.RangeError('Value out of range')
.setMinValue(1)
.setMaxValue(5);
console.log(error.message);
console.log(error instanceof Error);
console.log(error instanceof erx.RangeError);
console.log(error.toString());
console.log(error.minValue);
console.log(error.maxValue);
console.log(error.stack);
Extending custom error classes in ES6
class MyError extends ErrorEx {
constructor(...args) {
super(...args);
this.myproperty = 1000;
}
setFoo(val) {
this.set('foo', val);
}
}
throw new MyError('My message %d', 10)
.setFoo('fooooo')
.set({
prop1: 1,
prop2: 2
});
Extending custom error classes in ES5
function MyError() {
MyError.apply(this, arguments);
this.myproperty = 1000;
}
MyError.prototype = Object.create(ErrorEx.prototype);
MyError.prototype.constructor = MyError;
MyError.prototype.setFoo = function(val) {
this.set('foo', val);
};
throw new MyError('My message %d', 10).setFoo('fooooo');
Predefined error classes
-
ErrorEx: Base error class
-
ArgumentError: Extending from ErrorEx
-
RangeError: Extending from ErrorEx
-
HttpError: Extending from ErrorEx
-
HttpClientError: Extending from HttpError
-
HttpServerError: Extending from HttpError
Node Compatibility
License
MIT