npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egads

v1.0.2

Published

Yet another extensible error library for node.js

Downloads

1,433

Readme

npm version

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 native Error 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 error
var Err = require('egads').extend('Something done goofd', 500, 'GenericError');

//Extend it for cover you basic types
Err.auth = Err.extend('Unauthorized', 401, 'AuthError');

//Make specific errors
Err.auth.badToken = Err.auth.extend('Bad Auth Token');

//Use object parameters
Err.badInput = Err.extend({
    name : 'BadInputError',
    message : 'What am I suppose to do with this?',
    status : 400,
    fields : {
        shameOnYou : true
    }
})

module.exports = Err;

Throw'em

try{
    throw new Err.auth.badToken();
}catch(err){
    err instanceof Error;             //true
    err instanceof Err;               //true!
    err instanceof Err.auth;          //true!!
    err instanceof Err.auth.badToken; //true!!!
    
    err.name;       //'AuthError'
    err.message;    //'Bad Auth Token'
    err.status;     //401
    err.stack;      //Full stacktrace
    err.toString(); // 'AuthError : Bad Auth Token'
}

//Overide it!
try{
    //Leave out the `new` if you want
    throw Err.auth({
        status : 418,
        name : 'TeapotError',
        message : 'Entity body must be short and/or stout',
        fields : {
            type : 'Earl Grey',
            temp : 'hot'
        }
    });
}catch(err){
    err instanceof Err.auth;  //true!
    
    err.fields.type;          // 'Earl Grey'
    err.status;               //418
    err.toString();           // '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 = require('express')();
var ApiError = require('egads');

var AuthError = ApiError.extend({
    status : 400,
    name : 'AuthError'
});

app.get('/:coolGuy', (req, res) => {
    if(!req.params.coolGuy) throw AuthError('not cool enough');
    return res.send('yo');
});

//Express Error Handler
app.use((err, req, res, next) => {
    if(err instanceof ApiError){
        //Since this is an API Error, we know it will have a status, name, and message
        return res.status(err.status).send({
            type : err.name,
            message : err.message
        });
    }
    //If a generic error, print the whole stack for debugging
    return res.status(500).send({
        message : err.message,
        stack : err.stack
    });
});