Skip to content

Commit

Permalink
Terminate in progress response only on error
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 18, 2014
1 parent bd21b98 commit da0bc64
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Terminate in progress response only on error
* Use `on-finished` to determine request status

0.2.0 / 2014-09-03
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function finalhandler(req, res, options) {
return function (err) {
var msg

// ignore 404 on in-flight response
if (!err && res._header) {
debug('cannot 404 after headers sent')
return
}

// unhandled error
if (err) {
// default status code to 500
Expand Down
21 changes: 20 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,26 @@ describe('finalhandler(req, res)', function () {
var done = finalhandler(req, res)
res.statusCode = 301
res.write('0')
process.nextTick(done)
process.nextTick(function () {
done()
res.end('1')
})
})

request(server)
.get('/foo')
.expect(301, '01', done)
})

it('should terminate on error', function (done) {
var server = http.createServer(function (req, res) {
var done = finalhandler(req, res)
res.statusCode = 301
res.write('0')
process.nextTick(function () {
done(new Error('boom!'))
res.end('1')
})
})

request(server)
Expand Down

0 comments on commit da0bc64

Please sign in to comment.