errorhandler
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -0,1 +1,6 @@ | ||
1.3.0 / 2014-11-22 | ||
================== | ||
* Add `log` option | ||
1.2.3 / 2014-11-21 | ||
@@ -2,0 +7,0 @@ ================== |
40
index.js
@@ -6,2 +6,3 @@ /*! | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -12,2 +13,3 @@ */ | ||
* Module dependencies. | ||
* @private | ||
*/ | ||
@@ -22,2 +24,3 @@ | ||
* Module variables. | ||
* @private | ||
*/ | ||
@@ -28,2 +31,7 @@ | ||
/* istanbul ignore next */ | ||
var defer = typeof setImmediate === 'function' | ||
? setImmediate | ||
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } | ||
/** | ||
@@ -54,6 +62,25 @@ * Error handler: | ||
exports = module.exports = function errorHandler(){ | ||
exports = module.exports = function errorHandler(options) { | ||
// get environment | ||
var env = process.env.NODE_ENV || 'development' | ||
// get options | ||
var opts = options || {} | ||
// get log option | ||
var log = opts.log === undefined | ||
? env !== 'test' | ||
: opts.log | ||
if (typeof log !== 'function' && typeof log !== 'boolean') { | ||
throw new TypeError('option log must be function or boolean') | ||
} | ||
// default logging using console.error | ||
if (log === true) { | ||
log = function logerror(err, str) { | ||
console.error(str) | ||
} | ||
} | ||
return function errorHandler(err, req, res, next){ | ||
@@ -70,5 +97,6 @@ // respect err.status | ||
// write error to console | ||
if (env !== 'test') { | ||
console.error(stringify(err)) | ||
// log the error | ||
var str = stringify(err) | ||
if (log) { | ||
defer(log, err, str, req, res) | ||
} | ||
@@ -102,3 +130,3 @@ | ||
.replace('{statusCode}', res.statusCode) | ||
.replace(/\{error\}/g, escapeHtml(stringify(err)).replace(/ /g, ' ').replace(/\n/g, '<br>')); | ||
.replace(/\{error\}/g, escapeHtml(str).replace(/ /g, ' ').replace(/\n/g, '<br>')) | ||
res.setHeader('Content-Type', 'text/html; charset=utf-8'); | ||
@@ -118,3 +146,3 @@ res.end(html); | ||
res.setHeader('Content-Type', 'text/plain'); | ||
res.end(stringify(err)); | ||
res.end(str) | ||
} | ||
@@ -121,0 +149,0 @@ }; |
{ | ||
"name": "errorhandler", | ||
"description": "Development-only error handler middleware", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"contributors": [ | ||
@@ -16,3 +16,3 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"devDependencies": { | ||
"connect": "3", | ||
"after": "0.8.1", | ||
"istanbul": "0.3.2", | ||
@@ -19,0 +19,0 @@ "mocha": "~2.0.1", |
@@ -23,3 +23,3 @@ # errorhandler | ||
### errorhandler() | ||
### errorhandler([options]) | ||
@@ -31,4 +31,27 @@ Create new middleware to handle errors and respond with content negotiation. | ||
## Example | ||
#### options.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). | ||
```js | ||
@@ -46,2 +69,29 @@ var connect = require('connect') | ||
### 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. | ||
```js | ||
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 | ||
@@ -48,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11281
176
108