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-exceptions

v0.0.7

Published

Express Custom Errors and Exception reporting

Downloads

7

Readme

express-exceptions Build Status Coverage Status

Express middleware for handling Errors in both production and development mode.

This package comes with an exception reporting page, showing all types of information about the Error, where did it happen, your environment and system information. This report page is shown only if the application is running in development mode.

Here you can see the exception page example:

Screenshot

When running in production mode, it offers a Error page selection. This means that for different Error types you can show a different page.

This module was inspired by the node-verror and exception.

Installation

npm install express-exceptions --save

Usage

The usual setup of the module is to used it like a middleware. The exceptions variable is a function, that accepts 2 arguments, options object and the configuration object for the Error pages.

To use defaults just set it up with no arguments passed:

var express = require('express'),
    exceptions = require('express-exceptions');
    
    
var app = express();

app.use(exceptions());

options

exposeErrors

This property tells the module should it expose custom Errors to the global scope. Default: true

Read more about exposing errors in error-globals

logger

Attach a Logger instance that used by the application, so the module can automatically log errors. By default it is a noop function.

pagePrefix

If all the pages are located in a seperate folder, here you can define that folder prefix. Default: ''

showExceptionPage

If you do not wish to show an Exception Report page, set this to false. Default: true.

defaultErrorPage

Set a default Error page to be render, when an Error occurrs in production mode. Default: error

environment

Set the node environment. By default it checks if the process.env.NODE_ENV is set or it fallbacks to development

Configuration

This is the configaration object for selecting the Error pages. The key can be a String representing a name of the Error or the Error class itself. A value can be a String, representing the name of the page to be render by the Express Response, or it can be a Function that will be invoked from the module to enable specific error handling.


{
 'BadRequest': 'badRequestErrorPage',
 RuntimeError: function myCustomHandler(error, request, response, next) {
  error.property = 'my custom property';
  delete error.stack;
  
  res.json(error);
 }
}

If we want to configure it, so it will use the console as the log output, and all the error pages are located in a views/errors folder, and we have a error page selector configuration, we would do something like this.

var express = require('express'),
    exceptions = require('express-exceptions');
    
    
var app = express();

app.use(exceptions({
    logger: console,
    pagePrefix: 'errors'
}, {
    'NotFound': '404',
    'UnauthorizedError': 'unauthorized',
    'ForbiddenError': 'unauthorized'
}));

You can use this middleware more than once. Let's say that you have some REST routes in your application, and you do not want to show an HTML page, you can do something like this.

var express = require('express'),
    exceptions = require('express-exceptions');
    
    
var app = express();

// Use only when an error occurs on routes that start with /api/
app.use('/api/*', exceptions({
  showExceptionPage: false
}, {
  'Error': function(error, req, res, next) {
    
    // toJSON method will be invoked automatically
    return res.json(error);
  }
}
}));

// Use this as a default
app.use(exceptions());

This module also exposes:

exceptions.create(name, settings)

Type: Function

Returns: Error

This function creates a custom Error. Name argument is required. Settings default to:

  • logLevel: 'error'
  • statusCode: 500
  • init: noop

var exceptions = require('express-exceptions');

exceptions.create('MyCustomError', {
 logLevel: 'warn',
 statusCode: 500,
 init: function () {
  // custom initialization code
 }
})

For more information visit error-globals

exceptions.exceptionPageHandler(error, request, response, next)

Type: Function

This function renders the exception page report. If you only want to have exception during development stage, this is the function you should add to your middleware.


var express = require('express'),
    exceptions = require('express-exceptions');
    
    
var app = express();

app.use(exceptions.exceptionPageHandler);

License

Version 2.0