Skip to content

Commit b98aaa3

Browse files
kysnmjoyeecheung
authored andcommitted
net: migrate errors to internal/errors
Throw ERR_SOCKET_CLOSED and ERR_SERVER_NOT_RUNNING instead of the old-style errors in net.js. PR-URL: #17766 Refs: #17709 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8599465 commit b98aaa3

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

doc/api/errors.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,14 @@ Script execution was interrupted by `SIGINT` (For example, when Ctrl+C was press
13641364

13651365
The [`server.listen()`][] method was called while a `net.Server` was already
13661366
listening. This applies to all instances of `net.Server`, including HTTP, HTTPS,
1367-
and HTTP/2 Server instances.
1367+
and HTTP/2 `Server` instances.
1368+
1369+
<a id="ERR_SERVER_NOT_RUNNING"></a>
1370+
### ERR_SERVER_NOT_RUNNING
1371+
1372+
The [`server.close()`][] method was called when a `net.Server` was not
1373+
running. This applies to all instances of `net.Server`, including HTTP, HTTPS,
1374+
and HTTP/2 `Server` instances.
13681375

13691376
<a id="ERR_SOCKET_ALREADY_BOUND"></a>
13701377
### ERR_SOCKET_ALREADY_BOUND

lib/internal/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
451451
'Script execution was interrupted by `SIGINT`.');
452452
E('ERR_SERVER_ALREADY_LISTEN',
453453
'Listen method has been called more than once without closing.');
454+
E('ERR_SERVER_NOT_RUNNING', 'Server is not running.');
454455
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
455456
E('ERR_SOCKET_BAD_BUFFER_SIZE', 'Buffer size must be a positive integer');
456457
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536. Received %s.');

lib/net.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
744744
this._unrefTimer();
745745

746746
if (!this._handle) {
747-
this.destroy(new Error('This socket is closed'), cb);
747+
this.destroy(new errors.Error('ERR_SOCKET_CLOSED'), cb);
748748
return false;
749749
}
750750

@@ -1645,7 +1645,7 @@ Server.prototype.close = function(cb) {
16451645
if (typeof cb === 'function') {
16461646
if (!this._handle) {
16471647
this.once('close', function close() {
1648-
cb(new Error('Not running'));
1648+
cb(new errors.Error('ERR_SERVER_NOT_RUNNING'));
16491649
});
16501650
} else {
16511651
this.once('close', cb);

test/parallel/test-http-unix-socket.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ server.listen(common.PIPE, common.mustCall(function() {
5858
assert.strictEqual(res.body, 'hello world\n');
5959
server.close(common.mustCall(function(error) {
6060
assert.strictEqual(error, undefined);
61-
server.close(common.mustCall(function(error) {
62-
assert.strictEqual(error && error.message, 'Not running');
61+
server.close(common.expectsError({
62+
code: 'ERR_SERVER_NOT_RUNNING',
63+
message: 'Server is not running.',
64+
type: Error
6365
}));
6466
}));
6567
}));

test/parallel/test-net-socket-destroy-send.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ server.listen(0, common.mustCall(function() {
1212
conn.on('connect', common.mustCall(function() {
1313
// Test destroy returns this, even on multiple calls when it short-circuits.
1414
assert.strictEqual(conn, conn.destroy().destroy());
15-
conn.on('error', common.mustCall(function(err) {
16-
assert.strictEqual(err.message, 'This socket is closed');
15+
conn.on('error', common.expectsError({
16+
code: 'ERR_SOCKET_CLOSED',
17+
message: 'Socket is closed',
18+
type: Error
1719
}));
18-
conn.write(Buffer.from('kaboom'), common.mustCall(function(err) {
19-
assert.strictEqual(err.message, 'This socket is closed');
20+
21+
conn.write(Buffer.from('kaboom'), common.expectsError({
22+
code: 'ERR_SOCKET_CLOSED',
23+
message: 'Socket is closed',
24+
type: Error
2025
}));
2126
server.close();
2227
}));

test/parallel/test-net-socket-write-after-close.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ const net = require('net');
2626
server.listen(common.mustCall(() => {
2727
const port = server.address().port;
2828
const client = net.connect({ port }, common.mustCall(() => {
29-
client.on('error', common.mustCall((err) => {
30-
server.close();
31-
assert.strictEqual(err.constructor, Error);
32-
assert.strictEqual(err.message, 'This socket is closed');
29+
client.on('error', common.expectsError({
30+
code: 'ERR_SOCKET_CLOSED',
31+
message: 'Socket is closed',
32+
type: Error
3333
}));
34+
35+
server.close();
36+
3437
client._handle.close();
3538
client._handle = null;
3639
client.write('foo');

0 commit comments

Comments
 (0)