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 |
|
8 | Development-only error handler middleware.
|
9 |
|
10 | This middleware is only intended to be used in a development environment, as
|
11 | the _full error stack traces and internal details of any object passed to this
|
12 | module_ will be sent back to the client when an error occurs.
|
13 |
|
14 | When an object is provided to Express as an error, this module will display
|
15 | as much about this object as possible, and will do so by using content negotiation
|
16 | for 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 |
|
28 | This 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 |
|
39 |
|
40 | ```js
|
41 | var errorhandler = require('errorhandler')
|
42 | ```
|
43 |
|
44 | ### errorhandler(options)
|
45 |
|
46 | Create new middleware to handle errors and respond with content negotiation.
|
47 |
|
48 | #### Options
|
49 |
|
50 | Error handler accepts these properties in the options object.
|
51 |
|
52 | ##### log
|
53 |
|
54 | Provide a function to be called with the error and a string representation of
|
55 | the 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
|
58 | representation of the error, `req` is the request object and `res` is the
|
59 | response object (note, this function is invoked _after_ the response has been
|
60 | written).
|
61 |
|
62 | The default value for this option is `true` unless `process.env.NODE_ENV === 'test'`.
|
63 |
|
64 | Possible 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 |
|
74 | Basic example of adding this middleware as the error handler only in development
|
75 | with `connect` (`express` also can be used in this example).
|
76 |
|
77 | ```js
|
78 | var connect = require('connect')
|
79 | var errorhandler = require('errorhandler')
|
80 |
|
81 | var app = connect()
|
82 |
|
83 | if (process.env.NODE_ENV === 'development') {
|
84 | // only use in development
|
85 | app.use(errorhandler())
|
86 | }
|
87 | ```
|
88 |
|
89 | ### Custom output location
|
90 |
|
91 | Sometimes you may want to output the errors to a different location than STDERR
|
92 | during development, like a system notification, for example.
|
93 |
|
94 |
|
95 |
|
96 | ```js
|
97 | var connect = require('connect')
|
98 | var errorhandler = require('errorhandler')
|
99 | var notifier = require('node-notifier')
|
100 |
|
101 | var app = connect()
|
102 |
|
103 | if (process.env.NODE_ENV === 'development') {
|
104 | // only use in development
|
105 | app.use(errorhandler({ log: errorNotification }))
|
106 | }
|
107 |
|
108 | function 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
|