Skip to content

Commit 2f49796

Browse files
committed
Merge tag '1.2.4'
2 parents 7fd88a0 + 7383f0c commit 2f49796

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

HISTORY.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix heading content to not include stack
5+
16
1.3.1 / 2014-12-31
27
==================
38

@@ -10,6 +15,11 @@
1015

1116
* Add `log` option
1217

18+
1.2.4 / 2015-01-01
19+
==================
20+
21+
* Fix heading content to not include stack
22+
1323
1.2.3 / 2014-11-21
1424
==================
1525

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(The MIT License)
22

33
Copyright (c) 2014 Jonathan Ong <[email protected]>
4+
Copyright (c) 2014-2015 Douglas Christopher Wilson <[email protected]>
45

56
Permission is hereby granted, free of charge, to any person obtaining
67
a copy of this software and associated documentation files (the

index.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright(c) 2010 Sencha Inc.
44
* Copyright(c) 2011 TJ Holowaychuk
55
* Copyright(c) 2014 Jonathan Ong
6-
* Copyright(c) 2014 Douglas Christopher Wilson
6+
* Copyright(c) 2014-2015 Douglas Christopher Wilson
77
* MIT Licensed
88
*/
99

@@ -22,7 +22,9 @@ var util = require('util')
2222
* @private
2323
*/
2424

25+
var doubleSpaceGlobalRegExp = / /g
2526
var inspect = util.inspect
27+
var newLineGlobalRegExp = /\n/g
2628
var toString = Object.prototype.toString
2729

2830
/* istanbul ignore next */
@@ -111,17 +113,24 @@ exports = module.exports = function errorHandler(options) {
111113
if (e) return next(e);
112114
fs.readFile(__dirname + '/public/error.html', 'utf8', function(e, html){
113115
if (e) return next(e);
114-
var stack = String(err.stack || '')
115-
.split('\n').slice(1)
116-
.map(function(v){ return '<li>' + escapeHtml(v).replace(/ /g, ' &nbsp;') + '</li>'; }).join('');
117-
html = html
118-
.replace('{style}', style)
119-
.replace('{stack}', stack)
120-
.replace('{title}', escapeHtml(exports.title))
121-
.replace('{statusCode}', res.statusCode)
122-
.replace(/\{error\}/g, escapeHtml(str).replace(/ /g, ' &nbsp;').replace(/\n/g, '<br>'))
123-
res.setHeader('Content-Type', 'text/html; charset=utf-8');
124-
res.end(html);
116+
var isInspect = !err.stack && String(err) === toString.call(err)
117+
var errorHtml = !isInspect
118+
? escapeHtmlBlock(str.split('\n', 1)[0] || 'Error')
119+
: 'Error'
120+
var stack = !isInspect
121+
? String(str).split('\n').slice(1)
122+
: [str]
123+
var stackHtml = stack
124+
.map(function (v) { return '<li>' + escapeHtmlBlock(v) + '</li>' })
125+
.join('')
126+
var body = html
127+
.replace('{style}', style)
128+
.replace('{stack}', stackHtml)
129+
.replace('{title}', escapeHtml(exports.title))
130+
.replace('{statusCode}', res.statusCode)
131+
.replace(/\{error\}/g, errorHtml)
132+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
133+
res.end(body)
125134
});
126135
});
127136
// json
@@ -145,6 +154,17 @@ exports = module.exports = function errorHandler(options) {
145154

146155
exports.title = 'Connect';
147156

157+
/**
158+
* Escape a block of HTML, preserving whitespace.
159+
* @api private
160+
*/
161+
162+
function escapeHtmlBlock(str) {
163+
return escapeHtml(str)
164+
.replace(doubleSpaceGlobalRegExp, ' &nbsp;')
165+
.replace(newLineGlobalRegExp, '<br>')
166+
}
167+
148168
/**
149169
* Stringify a value.
150170
* @api private

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ describe('errorHandler()', function () {
106106
.get('/')
107107
.set('Accept', 'text/html')
108108
.expect('Content-Type', /text\/html/)
109-
.expect(/<title>/)
110-
.expect(/Error: boom!/)
111-
.expect(/ &nbsp; &nbsp;at/)
109+
.expect(/<title>Error: boom!<\/title>/)
110+
.expect(/<h2><em>500<\/em> Error: boom!<\/h2>/)
111+
.expect(/<li> &nbsp; &nbsp;at/)
112112
.end(done)
113113
})
114114
})

0 commit comments

Comments
 (0)