errorhandler
Advanced tools
Comparing version 1.4.3 to 1.5.0
@@ -0,1 +1,12 @@ | ||
1.5.0 / 2016-11-15 | ||
================== | ||
* Pretty print JSON error response | ||
* deps: accepts@~1.3.3 | ||
- deps: mime-types@~2.1.11 | ||
- deps: negotiator@0.6.1 | ||
* 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 | ||
@@ -2,0 +13,0 @@ ================== |
85
index.js
@@ -18,4 +18,5 @@ /*! | ||
var accepts = require('accepts') | ||
var escapeHtml = require('escape-html'); | ||
var fs = require('fs'); | ||
var escapeHtml = require('escape-html') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var util = require('util') | ||
@@ -28,5 +29,7 @@ | ||
var doubleSpaceGlobalRegExp = / /g | ||
var DOUBLE_SPACE_REGEXP = /\x20{2}/g | ||
var NEW_LINE_REGEXP = /\n/g | ||
var STYLESHEET = fs.readFileSync(path.join(__dirname, '/public/style.css'), 'utf8') | ||
var TEMPLATE = fs.readFileSync(path.join(__dirname, '/public/error.html'), 'utf8') | ||
var inspect = util.inspect | ||
var newLineGlobalRegExp = /\n/g | ||
var toString = Object.prototype.toString | ||
@@ -37,3 +40,3 @@ | ||
? setImmediate | ||
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } | ||
: function (fn) { process.nextTick(fn.bind.apply(fn, arguments)) } | ||
@@ -65,3 +68,3 @@ /** | ||
exports = module.exports = function errorHandler(options) { | ||
exports = module.exports = function errorHandler (options) { | ||
// get environment | ||
@@ -87,3 +90,3 @@ var env = process.env.NODE_ENV || 'development' | ||
return function errorHandler(err, req, res, next){ | ||
return function errorHandler (err, req, res, next) { | ||
// respect err.statusCode | ||
@@ -124,33 +127,27 @@ if (err.statusCode) { | ||
if (type === 'html') { | ||
fs.readFile(__dirname + '/public/style.css', 'utf8', function(e, style){ | ||
if (e) return next(e); | ||
fs.readFile(__dirname + '/public/error.html', 'utf8', function(e, html){ | ||
if (e) return next(e); | ||
var isInspect = !err.stack && String(err) === toString.call(err) | ||
var errorHtml = !isInspect | ||
? escapeHtmlBlock(str.split('\n', 1)[0] || 'Error') | ||
: 'Error' | ||
var stack = !isInspect | ||
? String(str).split('\n').slice(1) | ||
: [str] | ||
var stackHtml = stack | ||
.map(function (v) { return '<li>' + escapeHtmlBlock(v) + '</li>' }) | ||
.join('') | ||
var body = html | ||
.replace('{style}', style) | ||
.replace('{stack}', stackHtml) | ||
.replace('{title}', escapeHtml(exports.title)) | ||
.replace('{statusCode}', res.statusCode) | ||
.replace(/\{error\}/g, errorHtml) | ||
res.setHeader('Content-Type', 'text/html; charset=utf-8') | ||
res.end(body) | ||
}); | ||
}); | ||
var isInspect = !err.stack && String(err) === toString.call(err) | ||
var errorHtml = !isInspect | ||
? escapeHtmlBlock(str.split('\n', 1)[0] || 'Error') | ||
: 'Error' | ||
var stack = !isInspect | ||
? String(str).split('\n').slice(1) | ||
: [str] | ||
var stackHtml = stack | ||
.map(function (v) { return '<li>' + escapeHtmlBlock(v) + '</li>' }) | ||
.join('') | ||
var body = TEMPLATE | ||
.replace('{style}', STYLESHEET) | ||
.replace('{stack}', stackHtml) | ||
.replace('{title}', escapeHtml(exports.title)) | ||
.replace('{statusCode}', res.statusCode) | ||
.replace(/\{error\}/g, errorHtml) | ||
res.setHeader('Content-Type', 'text/html; charset=utf-8') | ||
res.end(body) | ||
// json | ||
} else if (type === 'json') { | ||
var error = { message: err.message, stack: err.stack }; | ||
for (var prop in err) error[prop] = err[prop]; | ||
var json = JSON.stringify({ error: error }); | ||
var error = { message: err.message, stack: err.stack } | ||
for (var prop in err) error[prop] = err[prop] | ||
var json = JSON.stringify({ error: error }, null, 2) | ||
res.setHeader('Content-Type', 'application/json; charset=utf-8') | ||
res.end(json); | ||
res.end(json) | ||
// plain text | ||
@@ -161,4 +158,4 @@ } else { | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
@@ -169,3 +166,3 @@ /** | ||
exports.title = 'Connect'; | ||
exports.title = 'Connect' | ||
@@ -177,6 +174,6 @@ /** | ||
function escapeHtmlBlock(str) { | ||
function escapeHtmlBlock (str) { | ||
return escapeHtml(str) | ||
.replace(doubleSpaceGlobalRegExp, ' ') | ||
.replace(newLineGlobalRegExp, '<br>') | ||
.replace(DOUBLE_SPACE_REGEXP, ' ') | ||
.replace(NEW_LINE_REGEXP, '<br>') | ||
} | ||
@@ -189,3 +186,3 @@ | ||
function stringify(val) { | ||
function stringify (val) { | ||
var stack = val.stack | ||
@@ -209,4 +206,4 @@ | ||
function logerror(err, str) { | ||
console.error(str) | ||
function logerror (err, str) { | ||
console.error(str || err.stack) | ||
} |
{ | ||
"name": "errorhandler", | ||
"description": "Development-only error handler middleware", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"contributors": [ | ||
@@ -12,9 +12,13 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"dependencies": { | ||
"accepts": "~1.3.0", | ||
"accepts": "~1.3.3", | ||
"escape-html": "~1.0.3" | ||
}, | ||
"devDependencies": { | ||
"after": "0.8.1", | ||
"istanbul": "0.4.2", | ||
"mocha": "2.3.4", | ||
"after": "0.8.2", | ||
"eslint": "3.10.2", | ||
"eslint-config-standard": "6.2.1", | ||
"eslint-plugin-promise": "3.3.2", | ||
"eslint-plugin-standard": "2.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "2.5.3", | ||
"supertest": "1.1.0" | ||
@@ -32,2 +36,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
@@ -34,0 +39,0 @@ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", |
@@ -29,2 +29,6 @@ # errorhandler | ||
This is a [Node.js](https://nodejs.org/en/) module available through the | ||
[npm registry](https://www.npmjs.com/). Installation is done using the | ||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): | ||
```sh | ||
@@ -31,0 +35,0 @@ $ npm install errorhandler |
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
15031
129
8
201
Updatedaccepts@~1.3.3