diff --git a/HISTORY.md b/HISTORY.md index 8d0f3c9..8ed36cf 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/index.js b/index.js index cea7ca3..bb2bb58 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/test.js b/test/test.js index 80fd0cc..1e6d61d 100644 --- a/test/test.js +++ b/test/test.js @@ -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)