From 1e7ccfab33fa9a7fafee754e558cf596d1f991bc Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 22 Sep 2021 10:06:50 +0200 Subject: [PATCH] [Backport 7.x] Show socket local/remote address in case of ECONNRESET (#1556) Co-authored-by: Tomas Della Vedova --- lib/Connection.js | 9 ++++++++- test/unit/connection.test.js | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Connection.js b/lib/Connection.js index 88a154ae6..feaa3ab23 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -113,7 +113,14 @@ class Connection { const onError = err => { cleanListeners() this._openRequests-- - callback(new ConnectionError(err.message), null) + let message = err.message + if (err.code === 'ECONNRESET') { + /* istanbul ignore next */ + const socket = request.socket || {} + /* istanbul ignore next */ + message += ` - Local: ${socket.localAddress || 'unknown'}:${socket.localPort || 'unknown'}, Remote: ${socket.remoteAddress || 'unknown'}:${socket.remotePort || 'unknown'}` + } + callback(new ConnectionError(message), null) } const onAbort = () => { diff --git a/test/unit/connection.test.js b/test/unit/connection.test.js index 5ba1c494a..a556564a0 100644 --- a/test/unit/connection.test.js +++ b/test/unit/connection.test.js @@ -1084,3 +1084,25 @@ test('getIssuerCertificate detects invalid/malformed certificates', t => { } t.equal(getIssuerCertificate(socket), null) }) + +test('Should show local/remote socket address in case of ECONNRESET', t => { + t.plan(2) + + function handler (req, res) { + res.destroy() + } + + buildServer(handler, ({ port }, server) => { + const connection = new Connection({ + url: new URL(`http://localhost:${port}`) + }) + connection.request({ + path: '/hello', + method: 'GET' + }, (err, res) => { + t.ok(err instanceof ConnectionError) + t.match(err.message, /socket\shang\sup\s-\sLocal:\s127.0.0.1:\d+,\sRemote:\s127.0.0.1:\d+/) + server.stop() + }) + }) +})