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

err-map

v1.0.2

Published

lightweight middleware for error filtering

Downloads

4

Readme

errMap

lightweight middleware for error filtering

Impetus

errMap was created to help control which errors I sent allowed out of an API I was building: i.e. I didn't want sensitive information leaking out to the client in the form of error messages, but I also wanted an easy interface for managing which errors I did allow.

Bad: SQL failure: INSERT id = 3, password_hash = 'fkdsljfa', mismatched_field = 2 INTO USERS

Good: Error: validation error: please check your fields

Installation

$ npm install err-map

Quick Start

var ErrMap = require('err-map');
var initialErrors = {
  'firstErr': {
    status: 400,
    message: 'you goofed it up!'
  }
};

//Will only be called if the error is raised via errMap.
var handler = function(errMapObj, req, res, next) {
  res.status(errMapObj.status).json({message: errMapObj.message});
}

var errMap = new ErrMap(initialErrors, handler);

//Add another error:
errMap.register('second error message', 402, '2ndAlias');


var app = require('express')();

//register a route:
app.get('/err', function() {
  throw errMap.raise('2ndAlias');
});

//instantiate errMap middleware, and the base (catch-all) error:
app.use(errMap.activate({
  status: 500,
  message: 'Internal Server Error'
}));



app.listen(3000);

API

Constructor: function ([map,] [handler])

Instantiate a new instance of errmap with a predetermined error map and handler function.

defaults:

map = {};
handler = function(errMapObj, req, res, next) {
    res.status(errMapObj.status).json({
      message: errMapObj.message
    });
  }

example:

var errMap = new ErrMap(initialErrors, handler);

Methods

activate: function([baseError])

Activate the middleware. Generally, this is the last piece of middleware used.

defaults:

baseError = {
    status: 500,
    message: 'Internal Server Error'
  };

example:

app.use(middleware1);
app.use(middleware2);
//etc
app.use(errMap.activate(baseError))

raise: function(message || alias)

Throw an error from a message or alias.

example:

app.get('/restricted', function() {
  throw errMap.raise('not allowed');
});

***NOTE: If you prefer, errors may also be raised with the standard syntax (It will still work :)):

throw new Error([alias || key])

register: function(message, status, [alias])

Attach a new error to the map, optionally accessed through the alias.

example:

errMap.register('API requires authentication', 401, 'noAuth')
contributors/feedback welcome.