Skip to content
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

net: migrate errors to internal/errors #17766

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,12 @@ The [`server.listen()`][] method was called while a `net.Server` was already
listening. This applies to all instances of `net.Server`, including HTTP, HTTPS,
and HTTP/2 Server instances.

<a id="ERR_SERVER_NOT_RUNNING"></a>
### ERR_SERVER_NOT_RUNNING

The [`server.close()`][] method was called when a `net.Server` was not running. This applies to all instances of `net.Server`, including HTTP, HTTPS,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap at 80 chars?

and HTTP/2 Server instances.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "Server" should probably be either in backticks or else lowercase. (Either it is referring to a server generically, in which case lowercase, or it is referring to the Server object, in which case backticks.)


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

Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s');
E('ERR_SERVER_ALREADY_LISTEN',
'Listen method has been called more than once without closing.');
E('ERR_SERVER_NOT_RUNNING', 'Server is not running.');
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
E('ERR_SOCKET_BAD_BUFFER_SIZE', 'Buffer size must be a positive integer');
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536. Received %s.');
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
this._unrefTimer();

if (!this._handle) {
this.destroy(new Error('This socket is closed'), cb);
this.destroy(new errors.Error('ERR_SOCKET_CLOSED'), cb);
return false;
}

Expand Down Expand Up @@ -1624,7 +1624,7 @@ Server.prototype.close = function(cb) {
if (typeof cb === 'function') {
if (!this._handle) {
this.once('close', function close() {
cb(new Error('Not running'));
cb(new errors.Error('ERR_SERVER_NOT_RUNNING'));
});
} else {
this.once('close', cb);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-unix-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ server.listen(common.PIPE, common.mustCall(function() {
server.close(common.mustCall(function(error) {
assert.strictEqual(error, undefined);
server.close(common.mustCall(function(error) {
assert.strictEqual(error && error.message, 'Not running');
assert.strictEqual(error && error.code, 'ERR_SERVER_NOT_RUNNING');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use common.expectsError() to test this? This should be something like

common.expectsError({
  code: 'ERR_SERVER_NOT_RUNNING',
  message: 'Server is not running.',
  type: Error
})(error);

}));
}));
}));
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-net-socket-destroy-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ server.listen(0, common.mustCall(function() {
// Test destroy returns this, even on multiple calls when it short-circuits.
assert.strictEqual(conn, conn.destroy().destroy());
conn.on('error', common.mustCall(function(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The common.mustCall is not necessary here. common.expectsError creates a function that can receive an error and also makes sure that it actually ran.

assert.strictEqual(err.message, 'This socket is closed');
assert.strictEqual(err.code, 'ERR_SOCKET_CLOSED');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, please use common.expectsError

}));
conn.write(Buffer.from('kaboom'), common.mustCall(function(err) {
assert.strictEqual(err.message, 'This socket is closed');
assert.strictEqual(err.code, 'ERR_SOCKET_CLOSED');
}));
server.close();
}));
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-net-socket-write-after-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const net = require('net');
const client = net.connect({ port }, common.mustCall(() => {
client.on('error', common.mustCall((err) => {
server.close();
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.message, 'This socket is closed');
assert.strictEqual(err.code, 'ERR_SOCKET_CLOSED');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

}));
client._handle.close();
client._handle = null;
Expand Down