Skip to content

Commit bc2c767

Browse files
authored
fix: gracefully handle when handling an error and socket is null (#57)
1 parent 3d4f55a commit bc2c767

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==================
3+
4+
* Gracefully handle when handling an error and socket is null
5+
16
1.2.0 / 2022-03-22
27
==================
38

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ function finalhandler (req, res, options) {
125125
// cannot actually respond
126126
if (headersSent(res)) {
127127
debug('cannot %d after headers sent', status)
128-
req.socket.destroy()
128+
if (req.socket) {
129+
req.socket.destroy()
130+
}
129131
return
130132
}
131133

test/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,25 @@ describe('finalhandler(req, res)', function () {
571571
})
572572
})
573573
})
574+
575+
if (parseInt(process.version.split('.')[0].replace(/^v/, ''), 10) > 11) {
576+
describe('req.socket', function () {
577+
it('should not throw when socket is null', function (done) {
578+
request(createServer(function (req, res, next) {
579+
res.statusCode = 200
580+
res.end('ok')
581+
process.nextTick(function () {
582+
req.socket = null
583+
next(new Error())
584+
})
585+
}))
586+
.get('/')
587+
.end(function () {
588+
assert.strictEqual(this.res.statusCode, 200)
589+
assert.strictEqual(this.res.text, 'ok')
590+
done()
591+
})
592+
})
593+
})
594+
}
574595
})

0 commit comments

Comments
 (0)