Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

errorhandler

expressjs9.3mMIT1.5.1TypeScript support: definitely-typed

Development-only error handler middleware

readme

errorhandler

NPM Version NPM Downloads Build Status Test Coverage

Development-only error handler middleware.

This middleware is only intended to be used in a development environment, as the full error stack traces and internal details of any object passed to this module will be sent back to the client when an error occurs.

When an object is provided to Express as an error, this module will display as much about this object as possible, and will do so by using content negotiation for the response between HTML, JSON, and plain text.

  • When the object is a standard Error object, the string provided by the stack property will be returned in HTML/text responses.
  • When the object is a non-Error object, the result of util.inspect will be returned in HTML/text responses.
  • For JSON responses, the result will be an object with all enumerable properties from the object in the response.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install errorhandler

API

var errorhandler = require('errorhandler')

errorhandler(options)

Create new middleware to handle errors and respond with content negotiation.

Options

Error handler accepts these properties in the options object.

log

Provide a function to be called with the error and a string representation of the error. Can be used to write the error to any desired location, or set to false to only send the error back in the response. Called as log(err, str, req, res) where err is the Error object, str is a string representation of the error, req is the request object and res is the response object (note, this function is invoked after the response has been written).

The default value for this option is true unless process.env.NODE_ENV === 'test'.

Possible values:

  • true: Log errors using console.error(str).
  • false: Only send the error back in the response.
  • A function: pass the error to a function for handling.

Examples

Simple example

Basic example of adding this middleware as the error handler only in development with connect (express also can be used in this example).

var connect = require('connect')
var errorhandler = require('errorhandler')

var app = connect()

if (process.env.NODE_ENV === 'development') {
  // only use in development
  app.use(errorhandler())
}

Custom output location

Sometimes you may want to output the errors to a different location than STDERR during development, like a system notification, for example.

var connect = require('connect')
var errorhandler = require('errorhandler')
var notifier = require('node-notifier')

var app = connect()

if (process.env.NODE_ENV === 'development') {
  // only use in development
  app.use(errorhandler({ log: errorNotification }))
}

function errorNotification (err, str, req) {
  var title = 'Error in ' + req.method + ' ' + req.url

  notifier.notify({
    title: title,
    message: str
  })
}

License

MIT

changelog

1.5.1 / 2019-05-08

1.5.0 / 2016-11-15

  • Pretty print JSON error response
  • deps: accepts@~1.3.3
  • perf: front-load HTML template and stylesheet at middleware construction
  • perf: only load template and stylesheet once
  • perf: resolve file paths at start up

1.4.3 / 2016-01-17

  • deps: accepts@~1.3.0
  • deps: escape-html@~1.0.3
    • perf: enable strict mode
    • perf: optimize string replacement
    • perf: use faster string coercion

1.4.2 / 2015-07-30

  • deps: accepts@~1.2.12
    • deps: mime-types@~2.1.4

1.4.1 / 2015-07-05

  • deps: accepts@~1.2.10
    • deps: mime-types@~2.1.2

1.4.0 / 2015-06-10

  • Add charset to the Content-Type header
  • Support statusCode property on Error objects
  • deps: accepts@~1.2.9
    • deps: mime-types@~2.1.1
    • deps: negotiator@0.5.3
    • perf: avoid argument reassignment & argument slice
    • perf: avoid negotiator recursive construction
    • perf: enable strict mode
    • perf: remove unnecessary bitwise operator
  • deps: escape-html@1.0.2

1.3.6 / 2015-05-14

1.3.5 / 2015-03-14

  • deps: accepts@~1.2.5
    • deps: mime-types@~2.0.10

1.3.4 / 2015-02-15

1.3.3 / 2015-01-31

  • deps: accepts@~1.2.3
    • deps: mime-types@~2.0.8

1.3.2 / 2015-01-01

  • Fix heading content to not include stack

1.3.1 / 2014-12-31

1.3.0 / 2014-11-22

  • Add log option

1.2.4 / 2015-01-01

  • Fix heading content to not include stack

1.2.3 / 2014-11-21

  • deps: accepts@~1.1.3
    • deps: mime-types@~2.0.3

1.2.2 / 2014-10-15

  • deps: accepts@~1.1.2

1.2.1 / 2014-10-12

1.2.0 / 2014-09-02

  • Display error using util.inspect if no other representation
  • deps: accepts@~1.1.0

1.1.1 / 2014-06-20

  • deps: accepts@~1.0.4
    • use mime-types

1.1.0 / 2014-06-16

  • Display error on console formatted like throw
  • Escape HTML with escape-html module
  • Escape HTML in stack trace
  • Escape HTML in title
  • Fix up edge cases with error sent in response
  • Set X-Content-Type-Options: nosniff header
  • Use accepts for negotiation

1.0.2 / 2014-06-05

  • Pass on errors from reading error files

1.0.1 / 2014-04-29

  • Clean up error CSS
  • Do not respond after headers sent

1.0.0 / 2014-03-03

  • Genesis from connect