Skip to content

Commit d5938e6

Browse files
committed
Replace env option with stacktrace option
1 parent d03b937 commit d5938e6

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
unreleased
22
==========
33

4+
* Add `stacktrace` option
45
* Add `text/plain` fallback response
6+
* Remove `env` option; use `stacktrace` option instead
57
* Send complete HTML document
68
* Set `X-Content-Type-Options: nosniff` header
79

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will
2525
write out a 404 response to the `res`. If it is truthy, an error response will
2626
be written out to the `res`, and `res.statusCode` is set from `err.status`.
2727

28-
#### options.env
29-
30-
By default, the environment is determined by `NODE_ENV` variable, but it can be
31-
overridden by this option.
32-
3328
#### options.onerror
3429

3530
Provide a function to be called with the `err` when it exists. Can be used for
3631
writing errors to a central location without excessive function generation. Called
3732
as `onerror(err, req, res)`.
3833

34+
#### options.stacktrace
35+
36+
Specify if the stack trace of the error should be included in the response. By
37+
default, this is false, but can be enabled when necessary (like in development).
38+
It is not recommend to enable this on a production deployment
39+
3940
## Examples
4041

4142
### always 404

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ module.exports = finalhandler
4141
function finalhandler(req, res, options) {
4242
options = options || {}
4343

44-
// get environment
45-
var env = options.env || process.env.NODE_ENV || 'development'
46-
4744
// get error callback
4845
var onerror = options.onerror
4946

47+
// get stack trace option
48+
var stacktrace = options.stacktrace || false;
49+
5050
return function (err) {
5151
var body
5252
var constructBody
@@ -65,9 +65,9 @@ function finalhandler(req, res, options) {
6565
}
6666

6767
// production gets a basic error message
68-
msg = env === 'production'
69-
? http.STATUS_CODES[res.statusCode]
70-
: err.stack || err.toString()
68+
msg = stacktrace
69+
? err.stack || String(err)
70+
: http.STATUS_CODES[res.statusCode]
7171
} else {
7272
res.statusCode = 404
7373
msg = 'Cannot ' + req.method + ' ' + (req.originalUrl || req.url)

test/test.js

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ describe('finalhandler(req, res)', function () {
7777
})
7878

7979
describe('error response', function () {
80-
it('should include error stack', function (done) {
80+
it('should not include stack trace', function (done) {
8181
var server = createServer(new Error('boom!'))
8282
request(server)
8383
.get('/foo')
84-
.expect(500, /Error: boom!.*at.*:[0-9]+:[0-9]+/, done)
84+
.expect(500, /Internal Server Error/, function (err, res) {
85+
if (err) return done(err)
86+
should(res.text).not.match(/boom!/)
87+
done()
88+
})
8589
})
8690

8791
it('should handle HEAD', function (done) {
@@ -99,26 +103,6 @@ describe('finalhandler(req, res)', function () {
99103
.expect(500, done)
100104
})
101105

102-
it('should handle non-error-objects', function (done) {
103-
var server = createServer('lame string')
104-
request(server)
105-
.get('/foo')
106-
.expect(500, /lame string/, done)
107-
})
108-
109-
it('should send staus code name when production', function (done) {
110-
var err = new Error('boom!')
111-
err.status = 501
112-
var server = createServer(err, {env: 'production'})
113-
request(server)
114-
.get('/foo')
115-
.expect(501, /Not Implemented/, function (err, res) {
116-
if (err) return done(err)
117-
should(res.text).not.match(/boom!/)
118-
done()
119-
})
120-
})
121-
122106
describe('when HTML acceptable', function () {
123107
it('should respond with HTML', function (done) {
124108
var server = createServer(new Error('boom!'))
@@ -128,14 +112,6 @@ describe('finalhandler(req, res)', function () {
128112
.expect('Content-Type', 'text/html; charset=utf-8')
129113
.expect(500, /<html/, done)
130114
})
131-
132-
it('should escape error stack', function (done) {
133-
var server = createServer(new Error('boom!'))
134-
request(server)
135-
.get('/foo')
136-
.set('Accept', 'text/html')
137-
.expect(500, /Error: boom!<br> &nbsp; &nbsp;at/, done)
138-
})
139115
})
140116

141117
describe('when HTML not acceptable', function () {
@@ -145,7 +121,7 @@ describe('finalhandler(req, res)', function () {
145121
.get('/foo')
146122
.set('Accept', 'application/x-bogus')
147123
.expect('Content-Type', 'text/plain; charset=utf-8')
148-
.expect(500, /Error: boom!\n at/, done)
124+
.expect(500, 'Internal Server Error\n', done)
149125
})
150126
})
151127

@@ -222,6 +198,40 @@ describe('finalhandler(req, res)', function () {
222198
})
223199
})
224200
})
201+
202+
describe('stacktrace', function () {
203+
it('should include error stack', function (done) {
204+
var server = createServer(new Error('boom!'), {stacktrace: true})
205+
request(server)
206+
.get('/foo')
207+
.expect(500, /Error: boom!.*at.*:[0-9]+:[0-9]+/, done)
208+
})
209+
210+
it('should escape error stack for HTML response', function (done) {
211+
var server = createServer(new Error('boom!'), {stacktrace: true})
212+
request(server)
213+
.get('/foo')
214+
.set('Accept', 'text/html')
215+
.expect(500, /Error: boom!<br> &nbsp; &nbsp;at/, done)
216+
})
217+
218+
it('should not escape error stack for plain text response', function (done) {
219+
var server = createServer(new Error('boom!'), {stacktrace: true})
220+
request(server)
221+
.get('/foo')
222+
.set('Accept', 'application/x-bogus')
223+
.expect('Content-Type', 'text/plain; charset=utf-8')
224+
.expect(500, /Error: boom!\n at/, done)
225+
})
226+
227+
it('should handle non-error-objects', function (done) {
228+
var server = createServer('lame string', {stacktrace: true})
229+
request(server)
230+
.get('/foo')
231+
.set('Accept', 'text/html')
232+
.expect(500, /lame string/, done)
233+
})
234+
})
225235
})
226236

227237
function createServer(err, opts) {

0 commit comments

Comments
 (0)