UNPKG

3.99 kBMarkdownView Raw
1# errorhandler
2
3[![NPM Version][npm-version-image]][npm-url]
4[![NPM Downloads][npm-downloads-image]][npm-url]
5[![Build Status][travis-image]][travis-url]
6[![Test Coverage][coveralls-image]][coveralls-url]
7
8Development-only error handler middleware.
9
10This middleware is only intended to be used in a development environment, as
11the _full error stack traces and internal details of any object passed to this
12module_ will be sent back to the client when an error occurs.
13
14When an object is provided to Express as an error, this module will display
15as much about this object as possible, and will do so by using content negotiation
16for the response between HTML, JSON, and plain text.
17
18 * When the object is a standard `Error` object, the string provided by the
19 `stack` property will be returned in HTML/text responses.
20 * When the object is a non-`Error` object, the result of
21 [util.inspect](https://nodejs.org/api/util.html#util_util_inspect_object_options)
22 will be returned in HTML/text responses.
23 * For JSON responses, the result will be an object with all enumerable properties
24 from the object in the response.
25
26## Install
27
28This is a [Node.js](https://nodejs.org/en/) module available through the
29[npm registry](https://www.npmjs.com/). Installation is done using the
30[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
31
32```sh
33$ npm install errorhandler
34```
35
36## API
37
38<!-- eslint-disable no-unused-vars -->
39
40```js
41var errorhandler = require('errorhandler')
42```
43
44### errorhandler(options)
45
46Create new middleware to handle errors and respond with content negotiation.
47
48#### Options
49
50Error handler accepts these properties in the options object.
51
52##### log
53
54Provide a function to be called with the error and a string representation of
55the error. Can be used to write the error to any desired location, or set to
56`false` to only send the error back in the response. Called as
57`log(err, str, req, res)` where `err` is the `Error` object, `str` is a string
58representation of the error, `req` is the request object and `res` is the
59response object (note, this function is invoked _after_ the response has been
60written).
61
62The default value for this option is `true` unless `process.env.NODE_ENV === 'test'`.
63
64Possible values:
65
66 * `true`: Log errors using `console.error(str)`.
67 * `false`: Only send the error back in the response.
68 * A function: pass the error to a function for handling.
69
70## Examples
71
72### Simple example
73
74Basic example of adding this middleware as the error handler only in development
75with `connect` (`express` also can be used in this example).
76
77```js
78var connect = require('connect')
79var errorhandler = require('errorhandler')
80
81var app = connect()
82
83if (process.env.NODE_ENV === 'development') {
84 // only use in development
85 app.use(errorhandler())
86}
87```
88
89### Custom output location
90
91Sometimes you may want to output the errors to a different location than STDERR
92during development, like a system notification, for example.
93
94<!-- eslint-disable handle-callback-err -->
95
96```js
97var connect = require('connect')
98var errorhandler = require('errorhandler')
99var notifier = require('node-notifier')
100
101var app = connect()
102
103if (process.env.NODE_ENV === 'development') {
104 // only use in development
105 app.use(errorhandler({ log: errorNotification }))
106}
107
108function errorNotification (err, str, req) {
109 var title = 'Error in ' + req.method + ' ' + req.url
110
111 notifier.notify({
112 title: title,
113 message: str
114 })
115}
116```
117
118## License
119
120[MIT](LICENSE)
121
122[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/errorhandler/master
123[coveralls-url]: https://coveralls.io/r/expressjs/errorhandler?branch=master
124[npm-downloads-image]: https://badgen.net/npm/dm/errorhandler
125[npm-url]: https://npmjs.org/package/errorhandler
126[npm-version-image]: https://badgen.net/npm/v/errorhandler
127[travis-image]: https://badgen.net/travis/expressjs/errorhandler/master
128[travis-url]: https://travis-ci.org/expressjs/errorhandler