Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Latest commit

 

History

History
110 lines (82 loc) · 3.05 KB

README.md

File metadata and controls

110 lines (82 loc) · 3.05 KB

ErrorEx

NPM Version NPM Downloads Build Status Test Coverage Dependencies DevDependencies

'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;

License

MIT