Skip to content

Commit

Permalink
Fix heading content to not include stack
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
dougwilson committed Jan 1, 2015
1 parent 3cfb87a commit 61b1f47
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
5 changes: 5 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.2.3 / 2014-11-21
==================

Expand Down
43 changes: 32 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ var util = require('util')
* Module variables.
*/

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

/**
Expand Down Expand Up @@ -85,17 +87,25 @@ exports = module.exports = function errorHandler(){
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(stringify(err)).replace(/ /g, ' &nbsp;').replace(/\n/g, '<br>'));
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(html);
var str = stringify(err)
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 @@ -119,6 +129,17 @@ exports = module.exports = function errorHandler(){

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 @@ -58,9 +58,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 61b1f47

Please sign in to comment.