Skip to content

Commit

Permalink
Merge tag '1.2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jan 1, 2015
2 parents 7fd88a0 + 7383f0c commit 2f49796
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
10 changes: 10 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix heading content to not include stack

1.3.1 / 2014-12-31
==================

Expand All @@ -10,6 +15,11 @@

* Add `log` option

1.2.4 / 2015-01-01
==================

* Fix heading content to not include stack

1.2.3 / 2014-11-21
==================

Expand Down
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(The MIT License)

Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
44 changes: 32 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright(c) 2010 Sencha Inc.
* Copyright(c) 2011 TJ Holowaychuk
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014 Douglas Christopher Wilson
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/

Expand All @@ -22,7 +22,9 @@ var util = require('util')
* @private
*/

var doubleSpaceGlobalRegExp = / /g
var inspect = util.inspect
var newLineGlobalRegExp = /\n/g
var toString = Object.prototype.toString

/* istanbul ignore next */
Expand Down Expand Up @@ -111,17 +113,24 @@ exports = module.exports = function errorHandler(options) {
if (e) return next(e);
fs.readFile(__dirname + '/public/error.html', 'utf8', function(e, html){
if (e) return next(e);
var stack = String(err.stack || '')
.split('\n').slice(1)
.map(function(v){ return '<li>' + escapeHtml(v).replace(/ /g, ' &nbsp;') + '</li>'; }).join('');
html = html
.replace('{style}', style)
.replace('{stack}', stack)
.replace('{title}', escapeHtml(exports.title))
.replace('{statusCode}', res.statusCode)
.replace(/\{error\}/g, escapeHtml(str).replace(/ /g, ' &nbsp;').replace(/\n/g, '<br>'))
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(html);
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)
});
});
// json
Expand All @@ -145,6 +154,17 @@ exports = module.exports = function errorHandler(options) {

exports.title = 'Connect';

/**
* Escape a block of HTML, preserving whitespace.
* @api private
*/

function escapeHtmlBlock(str) {
return escapeHtml(str)
.replace(doubleSpaceGlobalRegExp, ' &nbsp;')
.replace(newLineGlobalRegExp, '<br>')
}

/**
* Stringify a value.
* @api private
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ describe('errorHandler()', function () {
.get('/')
.set('Accept', 'text/html')
.expect('Content-Type', /text\/html/)
.expect(/<title>/)
.expect(/Error: boom!/)
.expect(/ &nbsp; &nbsp;at/)
.expect(/<title>Error: boom!<\/title>/)
.expect(/<h2><em>500<\/em> Error: boom!<\/h2>/)
.expect(/<li> &nbsp; &nbsp;at/)
.end(done)
})
})
Expand Down

0 comments on commit 2f49796

Please sign in to comment.