3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
9 Development-only error handler middleware.
11 This middleware is only intended to be used in a development environment, as
12 the _full error stack traces and internal details of any object passed to this
13 module_ will be sent back to the client when an error occurs.
15 When an object is provided to Express as an error, this module will display
16 as much about this object as possible, and will do so by using content negotiation
17 for the response between HTML, JSON, and plain text.
19 * When the object is a standard `Error` object, the string provided by the
20 `stack` property will be returned in HTML/text responses.
21 * When the object is a non-`Error` object, the result of
22 [util.inspect](https://nodejs.org/api/util.html#util_util_inspect_object_options)
23 will be returned in HTML/text responses.
24 * For JSON responses, the result will be an object with all enumerable properties
25 from the object in the response.
30 $ npm install errorhandler
36 var errorhandler = require('errorhandler')
39 ### errorhandler(options)
41 Create new middleware to handle errors and respond with content negotiation.
45 Error handler accepts these properties in the options object.
49 Provide a function to be called with the error and a string representation of
50 the error. Can be used to write the error to any desired location, or set to
51 `false` to only send the error back in the response. Called as
52 `log(err, str, req, res)` where `err` is the `Error` object, `str` is a string
53 representation of the error, `req` is the request object and `res` is the
54 response object (note, this function is invoked _after_ the response has been
57 The default value for this option is `true` unless `process.env.NODE_ENV === 'test'`.
61 * `true`: Log errors using `console.error(str)`.
62 * `false`: Only send the error back in the response.
63 * A function: pass the error to a function for handling.
69 Basic example of adding this middleware as the error handler only in development
70 with `connect` (`express` also can be used in this example).
73 var connect = require('connect')
74 var errorhandler = require('errorhandler')
78 if (process.env.NODE_ENV === 'development') {
79 // only use in development
80 app.use(errorhandler())
84 ### Custom output location
86 Sometimes you may want to output the errors to a different location than STDERR
87 during development, like a system notification, for example.
90 var connect = require('connect')
91 var errorhandler = require('errorhandler')
92 var notifier = require('node-notifier')
96 if (process.env.NODE_ENV === 'development') {
97 // only use in development
98 app.use(errorhandler({log: errorNotification}))
101 function errorNotification(err, str, req) {
102 var title = 'Error in ' + req.method + ' ' + req.url
115 [npm-image]: https://img.shields.io/npm/v/errorhandler.svg
116 [npm-url]: https://npmjs.org/package/errorhandler
117 [travis-image]: https://img.shields.io/travis/expressjs/errorhandler/master.svg
118 [travis-url]: https://travis-ci.org/expressjs/errorhandler
119 [coveralls-image]: https://img.shields.io/coveralls/expressjs/errorhandler/master.svg
120 [coveralls-url]: https://coveralls.io/r/expressjs/errorhandler?branch=master
121 [downloads-image]: https://img.shields.io/npm/dm/errorhandler.svg
122 [downloads-url]: https://npmjs.org/package/errorhandler
123 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
124 [gratipay-url]: https://www.gratipay.com/dougwilson/