Skip to content

Commit

Permalink
fix: gracefully handle when handling an error and socket is null (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd authored Sep 2, 2024
1 parent 3d4f55a commit bc2c767
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==================

* Gracefully handle when handling an error and socket is null

1.2.0 / 2022-03-22
==================

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ function finalhandler (req, res, options) {
// cannot actually respond
if (headersSent(res)) {
debug('cannot %d after headers sent', status)
req.socket.destroy()
if (req.socket) {
req.socket.destroy()
}
return
}

Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,25 @@ describe('finalhandler(req, res)', function () {
})
})
})

if (parseInt(process.version.split('.')[0].replace(/^v/, ''), 10) > 11) {
describe('req.socket', function () {
it('should not throw when socket is null', function (done) {
request(createServer(function (req, res, next) {
res.statusCode = 200
res.end('ok')
process.nextTick(function () {
req.socket = null
next(new Error())
})
}))
.get('/')
.end(function () {
assert.strictEqual(this.res.statusCode, 200)
assert.strictEqual(this.res.text, 'ok')
done()
})
})
})
}
})

0 comments on commit bc2c767

Please sign in to comment.