errorex
v2.3.2
Published
'Extensible Error Class' implementation and predefined additional error types for javascript
Downloads
7,037
Maintainers
Readme
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); // Value out of range
console.log(error instanceof Error); // true
console.log(error instanceof erx.RangeError); // true
console.log(error.toString()); // RangeError: Value out of range
console.log(error.minValue); // 1
console.log(error.maxValue); // 5
console.log(error.stack); // Prints stack trace
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() {
ErrorEx.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
- node
>= 0.10
;