From d0a8bcc9430755fe661a81a20cc7c7d7579cae1b Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Mon, 8 Nov 2021 20:56:53 +0000 Subject: [PATCH] Treat aborts the same as before node 16 Cherry-pick 79376fd8436a0a7031e305cf80840fd01b5054a1 --- request.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/request.js b/request.js index 2d53e4b04..e1679f0a1 100644 --- a/request.js +++ b/request.js @@ -1510,6 +1510,14 @@ Request.prototype.onRequestResponse = function (response) { self.emit('end', chunk) }) responseContent.on('error', function (error) { + if (error.code === 'ECONNRESET' && error.message === 'aborted' && self.listenerCount('error') === 0) { + // Node 16 causes aborts to emit errors if there is an error listener. + // Without this short-circuit, it will cause unhandled exceptions since + // there is not always an `error` listener on `self`, but there will + // always be an `error` listener on `responseContent`. + // @see https://github.com/nodejs/node/pull/33172 + return + } self.emit('error', error) }) responseContent.on('close', function () { self.emit('close') })