Skip to content

Commit 1e7ccfa

Browse files
[Backport 7.x] Show socket local/remote address in case of ECONNRESET (#1556)
Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent 1362fb5 commit 1e7ccfa

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/Connection.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ class Connection {
113113
const onError = err => {
114114
cleanListeners()
115115
this._openRequests--
116-
callback(new ConnectionError(err.message), null)
116+
let message = err.message
117+
if (err.code === 'ECONNRESET') {
118+
/* istanbul ignore next */
119+
const socket = request.socket || {}
120+
/* istanbul ignore next */
121+
message += ` - Local: ${socket.localAddress || 'unknown'}:${socket.localPort || 'unknown'}, Remote: ${socket.remoteAddress || 'unknown'}:${socket.remotePort || 'unknown'}`
122+
}
123+
callback(new ConnectionError(message), null)
117124
}
118125

119126
const onAbort = () => {

test/unit/connection.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,3 +1084,25 @@ test('getIssuerCertificate detects invalid/malformed certificates', t => {
10841084
}
10851085
t.equal(getIssuerCertificate(socket), null)
10861086
})
1087+
1088+
test('Should show local/remote socket address in case of ECONNRESET', t => {
1089+
t.plan(2)
1090+
1091+
function handler (req, res) {
1092+
res.destroy()
1093+
}
1094+
1095+
buildServer(handler, ({ port }, server) => {
1096+
const connection = new Connection({
1097+
url: new URL(`http://localhost:${port}`)
1098+
})
1099+
connection.request({
1100+
path: '/hello',
1101+
method: 'GET'
1102+
}, (err, res) => {
1103+
t.ok(err instanceof ConnectionError)
1104+
t.match(err.message, /socket\shang\sup\s-\sLocal:\s127.0.0.1:\d+,\sRemote:\s127.0.0.1:\d+/)
1105+
server.stop()
1106+
})
1107+
})
1108+
})

0 commit comments

Comments
 (0)