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

express-simple-errors

v1.0.0

Published

Simple error handling middleware for NodeJS

Downloads

23

Readme

Express Simple Errors

Codeship Status for kellyjandrews/express-simple-errors Jest Code Coverage semantic-release

Using Error Handling Middleware

import express from 'express';
import ErrorHandler, { errors } from 'express-simple-errors';

const app = express();
const errorHandler = new ErrorHandler();

app.use(errorHandler.middleware());

The middleware method will handle NotFound errors, all errors from routing, and then send the response. You can use these as one simple function, or you can use them individually.

import express from 'express';
import ErrorHandler, { errors } from 'express-simple-errors';

const app = express();
const errorHandler = new ErrorHandler();

app.use(errorHandler.handleNotFound()); //creates new NotFound() error
app.use(errorHandler.handleError()); // builds error response and stores it in res.locals.errors
app.use(errorHandler.sendResponse()); //sends error code and response

You could inject middleware after the handleError method. The original err object will be intact for you to consume before passing to sendResponse.

The stackTrace is included with the default handler method. You can turn this off when you initialize.

const errorHandler = new ErrorHandler({stackTrace: false});

Customizing the Response Object

You can modify the default handler, or create a custom one for more specific needs. The handlers are called based on the errors name property. All error classes included with this module are "Error", and the handler used will be "Default".

const defaultHandler = (err, stack) => {
  const res = {};
  res.status= err.name;
  res.message= err.message;
  res.code= err.code;
  if (stack) res.stackTrace= err.stack;
  return res;
};

Each handler will be passed two parameters - the err object, and the stackTrace flag.

To modify the Default handler, or create a custom handler, you can use the setHandler method.


errors.setHandler('Default', (err, stack) => {
  return {
    status: 'error',
    data: err.message
  }
});

errors.setHandler('Custom', (err, stack) => ('You have encountered an error.')});

Creating Error Responses

import { errors } from 'express-simple-errors';

// basic example
function foo(bar) {
  if !(bar) throw new errors.Conflict();
}

//custom message
function foo(bar) {
  if !(bar) throw new errors.Conflict('You are missing something!');
}

//used as route middleware functions
function checkUser(req, res, next) {
  try {
    const user = //do stuff to check for unser;
    if (!user) return next(new errors.NotFound('This user does not exist'));
    next();
  } catch(e) {
    next(e);
  }
}

router.route(/)
  .get(checkUser)

Supported Error Codes

  • 400 BadRequest
  • 401 Unauthorized
  • 403 Forbidden
  • 404 NotFound
  • 409 Conflict
  • 415 UnsupportedMediaType