egads
Yet another extensible error library for node.js
Extensible, Generic, Agnostic, Descriptive, Simple
Why this library?
There are tons of alterntive libraries out there, however they didn't meet the feature set I wanted:
- Super tiny (under 40 lines!)
- Doesn't pollute global scope or mutate existing types
- Configurable and overwritable name, message, status, and custom fields
- Deep nesting. Each extended error can further extend
- Each error is an
instanceOf
all of it's parents, including the nativeError
type - Don't have to use the
new
keyword when throwing - Can use a single object parameter or multiple parameters
- Can access the error stack, eg.
err.stack
How To Use
Install npm install egads
Define your errors.
//define your base errorvar Err = ; //Extend it for cover you basic typesErrauth = Err; //Make specific errorsErrauthbadToken = Errauth; //Use object parametersErrbadInput = Err moduleexports = Err;
Throw'em
try throw ;catcherr err instanceof Error; //true err instanceof Err; //true! err instanceof Errauth; //true!! err instanceof ErrauthbadToken; //true!!! errname; //'AuthError' errmessage; //'Bad Auth Token' errstatus; //401 errstack; //Full stacktrace err; // 'AuthError : Bad Auth Token' //Overide it!try //Leave out the `new` if you want throw Err;catcherr err instanceof Errauth; //true! errfieldstype; // 'Earl Grey' errstatus; //418 err; // 'TeapotError : Entity body may be short and/or stout'
Express Handler
If you using express you can write a simple error handler for egads
errors.
var app = ;var ApiError = ; var AuthError = ApiError; app; //Express Error Handlerapp;