From 1aa5e97ca4e4fa77453bbee5b7b6267632d9ed69 Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Thu, 22 Dec 2022 18:16:53 +0100 Subject: [PATCH] Fixups --- doc/api/errors.md | 8 ++++++++ lib/internal/errors.js | 3 +++ lib/net.js | 3 ++- ....js => test-net-write-cb-on-destroy-before-connect.js} | 4 +++- 4 files changed, 16 insertions(+), 2 deletions(-) rename test/parallel/{test-net-write-cb-on-destroy.js => test-net-write-cb-on-destroy-before-connect.js} (81%) diff --git a/doc/api/errors.md b/doc/api/errors.md index b277a53e66cd6d..3271c818c14a5f 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2573,6 +2573,13 @@ could not be determined. An attempt was made to operate on an already closed socket. + + +### `ERR_SOCKET_CLOSED_BEFORE_CONNECTION` + +When calling [`net.Socket.write()`][] on a connecting socket and the socket was +closed before the connection was established. + ### `ERR_SOCKET_DGRAM_IS_CONNECTED` @@ -3586,6 +3593,7 @@ The native call from `process.cpuUsage` could not be processed. [`http`]: http.md [`https`]: https.md [`libuv Error handling`]: https://docs.libuv.org/en/v1.x/errors.html +[`net.Socket.write()`]: net.md#socketwritedata-encoding-callback [`net`]: net.md [`new URL(input)`]: url.md#new-urlinput-base [`new URLSearchParams(iterable)`]: url.md#new-urlsearchparamsiterable diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 5820747e0d7fcf..63e7f522be7a01 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1566,6 +1566,9 @@ E('ERR_SOCKET_BUFFER_SIZE', 'Could not get or set buffer size', SystemError); E('ERR_SOCKET_CLOSED', 'Socket is closed', Error); +E('ERR_SOCKET_CLOSED_BEFORE_CONNECTION', + 'Socket was closed before connection established', + Error); E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error); E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error); E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error); diff --git a/lib/net.js b/lib/net.js index 66049214fe881c..472352024bd651 100644 --- a/lib/net.js +++ b/lib/net.js @@ -98,6 +98,7 @@ const { ERR_SERVER_ALREADY_LISTEN, ERR_SERVER_NOT_RUNNING, ERR_SOCKET_CLOSED, + ERR_SOCKET_CLOSED_BEFORE_CONNECTION, ERR_MISSING_ARGS, }, aggregateErrors, @@ -900,7 +901,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) { this._writeGeneric(writev, data, encoding, cb); }); function onClose() { - cb(new ERR_SOCKET_CLOSED()); + cb(new ERR_SOCKET_CLOSED_BEFORE_CONNECTION()); } this.once('close', onClose); return; diff --git a/test/parallel/test-net-write-cb-on-destroy.js b/test/parallel/test-net-write-cb-on-destroy-before-connect.js similarity index 81% rename from test/parallel/test-net-write-cb-on-destroy.js rename to test/parallel/test-net-write-cb-on-destroy-before-connect.js index 92d1e58f3bfe8a..99efb660346c4f 100644 --- a/test/parallel/test-net-write-cb-on-destroy.js +++ b/test/parallel/test-net-write-cb-on-destroy-before-connect.js @@ -8,6 +8,8 @@ const server = net.createServer(); server.listen(0, common.mustCall(() => { const socket = new net.Socket(); + socket.on('connect', common.mustNotCall()); + socket.connect({ port: server.address().port, }); @@ -15,7 +17,7 @@ server.listen(0, common.mustCall(() => { assert(socket.connecting); socket.write('foo', common.expectsError({ - code: 'ERR_SOCKET_CLOSED', + code: 'ERR_SOCKET_CLOSED_BEFORE_CONNECTION', name: 'Error' }));