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

oops-error-handler

v1.0.6

Published

Error handler middleware for Express

Downloads

1

Readme

oops-error-handler

This package provides an error handling middleware for Express applications.

Installation

To install this package, use npm:

npm install oops-error-handler

Usage

The error handling middleware is used to handle errors that occur during request processing in your Express application. To use the middleware, import it into your application:

const errorHandler = require('oops-error-handler');

Then, add it as middleware to your Express application:

app.use(errorHandler());

This will add the error handling middleware to your application's middleware stack. When an error occurs during request processing, the middleware will catch it and handle it appropriately.

Note that this middleware must be placed at the end of the middleware function stack, after all other middleware and route handlers.

Customization

The error handling middleware can be customized by passing options to the errorHandler function. The available options are:

  • handlers: An object that maps error names to custom error handling functions.
  • logger: A function that logs error messages.
  • reporter: A function that reports errors to an external service.
  • formaters: An object that maps error formats to formatting functions.
const errorHandler = require('oops-error-handler');

app.use(errorHandler({
  handlers: {
    'NotFoundError': (error, res) => {
      res.status(404).send('Not found');
    },
    'UnauthorizedError': (error, res) => {
      res.status(401).send('Unauthorized');
    }
  },
  logger: (level, message, metadata) => {
    console.log(`[${level}] ${message}`, metadata);
  },
  reporter: (error) => {
    // Send error report to external service
  },
  formaters: {
    'html': (error) => {
      return `<html><body><h1>${error.message}</h1></body></html>`;
    },
    'json': (error) => {
      return {
        error: {
          message: error.message,
          code: error.code
        }
      };
    }
  }
}));

Writing custom error handlers

You can write custom error handlers by adding them to the handlers object. Error handlers are functions that take three arguments: the error object, the response object, and the options object.

const errorHandler = require('express-error-handler-middleware');
const MyCustomError = require('./my-custom-error');

app.use(errorHandler({
  handlers: {
    'MyCustomError': (error, res) => {
      res.status(400).send(error.message);
    }
  }
}));

app.get('/', (req, res, next) => {
  const error = new MyCustomError('Something went wrong');
  next(error);
});

In this example, a custom error handler is added for a MyCustomError error. When this error is thrown in a request handler, the error handler will be invoked with the error object and the response object. The error handler can then use the response object to send an appropriate response to the client.

Writing custom error formats

You can write custom error formats by adding them to the formaters object. Error formats are functions that take the error object as an argument and return a formatted error response.

const errorHandler = require('express-error-handler-middleware');

app.use(errorHandler({
  formaters: {
    'xml': (error) => {
      return `<error><message>${error.message}</message><code>${error.code}</code></error>`;
    }
  }
}));

In this example, a custom error format is added for an xml format. When an error is thrown and the client has requested an xml response format, the error formatter will be invoked with the error object and the formatted error response will be returned to the client.

Default error handling behavior

If no custom error handlers or formatters are specified, the error handling middleware will use the default error handling behavior. The default error handling behavior logs errors with a severity level of 'warn' for client errors (status codes 400-499) and 'error' for server errors (status codes 500 and above). It also returns a JSON response with the error message and status code.

License

This package is licensed under the GPL-3 license.