From 3ee37329da7323a86c248a63fe20d40885c7a84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20F=2E=20Romaniello?= Date: Wed, 29 Jun 2016 10:29:19 -0300 Subject: [PATCH] tls: add host and port info to ECONNRESET errors Add more information to the "ECONNRESET" errors generated when the socket hang ups before establishing the secure connection. These kind of errors are really hard to troubleshoot without this info. PR-URL: https://github.com/nodejs/node/pull/7476 Reviewed-By: Trevor Norris Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: Fedor Indutny Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno Reviewed-By: Yazhong Liu --- lib/_tls_wrap.js | 4 +++ .../test-tls-wrap-econnreset-localaddress.js | 25 ++++++++++++++++++ .../parallel/test-tls-wrap-econnreset-pipe.js | 22 ++++++++++++++++ .../test-tls-wrap-econnreset-socket.js | 26 +++++++++++++++++++ test/parallel/test-tls-wrap-econnreset.js | 22 ++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 test/parallel/test-tls-wrap-econnreset-localaddress.js create mode 100644 test/parallel/test-tls-wrap-econnreset-pipe.js create mode 100644 test/parallel/test-tls-wrap-econnreset-socket.js create mode 100644 test/parallel/test-tls-wrap-econnreset.js diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index eba8aada15..aba633a8ac 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -1129,6 +1129,10 @@ exports.connect = function(...args /* [port,] [host,] [options,] [cb] */) { socket._hadError = true; var error = new Error('socket hang up'); error.code = 'ECONNRESET'; + error.path = options.path; + error.host = options.host; + error.port = options.port; + error.localAddress = options.localAddress; socket.destroy(error); } } diff --git a/test/parallel/test-tls-wrap-econnreset-localaddress.js b/test/parallel/test-tls-wrap-econnreset-localaddress.js new file mode 100644 index 0000000000..981e57248a --- /dev/null +++ b/test/parallel/test-tls-wrap-econnreset-localaddress.js @@ -0,0 +1,25 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); +const tls = require('tls'); + +const server = net.createServer((c) => { + c.end(); +}).listen(common.mustCall(() => { + const port = server.address().port; + + tls.connect({ + port: port, + localAddress: common.localhostIPv4 + }, common.localhostIPv4) + .once('error', common.mustCall((e) => { + assert.strictEqual(e.code, 'ECONNRESET'); + assert.strictEqual(e.path, undefined); + assert.strictEqual(e.host, undefined); + assert.strictEqual(e.port, port); + assert.strictEqual(e.localAddress, common.localhostIPv4); + server.close(); + })); +})); diff --git a/test/parallel/test-tls-wrap-econnreset-pipe.js b/test/parallel/test-tls-wrap-econnreset-pipe.js new file mode 100644 index 0000000000..5925d65658 --- /dev/null +++ b/test/parallel/test-tls-wrap-econnreset-pipe.js @@ -0,0 +1,22 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const tls = require('tls'); +const net = require('net'); + +common.refreshTmpDir(); + +const server = net.createServer((c) => { + c.end(); +}).listen(common.PIPE, common.mustCall(() => { + tls.connect({ path: common.PIPE }) + .once('error', common.mustCall((e) => { + assert.strictEqual(e.code, 'ECONNRESET'); + assert.strictEqual(e.path, common.PIPE); + assert.strictEqual(e.port, undefined); + assert.strictEqual(e.host, undefined); + assert.strictEqual(e.localAddress, undefined); + server.close(); + })); +})); diff --git a/test/parallel/test-tls-wrap-econnreset-socket.js b/test/parallel/test-tls-wrap-econnreset-socket.js new file mode 100644 index 0000000000..ba7511c194 --- /dev/null +++ b/test/parallel/test-tls-wrap-econnreset-socket.js @@ -0,0 +1,26 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); +const tls = require('tls'); + +const server = net.createServer((c) => { + c.end(); +}).listen(common.mustCall(() => { + const port = server.address().port; + + const socket = new net.Socket(); + + tls.connect({ socket }) + .once('error', common.mustCall((e) => { + assert.strictEqual(e.code, 'ECONNRESET'); + assert.strictEqual(e.path, undefined); + assert.strictEqual(e.host, undefined); + assert.strictEqual(e.port, undefined); + assert.strictEqual(e.localAddress, undefined); + server.close(); + })); + + socket.connect(port); +})); diff --git a/test/parallel/test-tls-wrap-econnreset.js b/test/parallel/test-tls-wrap-econnreset.js new file mode 100644 index 0000000000..07fdaf6b22 --- /dev/null +++ b/test/parallel/test-tls-wrap-econnreset.js @@ -0,0 +1,22 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); +const tls = require('tls'); + +const server = net.createServer((c) => { + c.end(); +}).listen(common.mustCall(() => { + const port = server.address().port; + + tls.connect(port, common.localhostIPv4) + .once('error', common.mustCall((e) => { + assert.strictEqual(e.code, 'ECONNRESET'); + assert.strictEqual(e.path, undefined); + assert.strictEqual(e.host, common.localhostIPv4); + assert.strictEqual(e.port, port); + assert.strictEqual(e.localAddress, undefined); + server.close(); + })); +}));