Skip to content

[Backport 7.x] Show socket local/remote address in case of ECONNRESET #1556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})