diff --git a/doc/api/errors.md b/doc/api/errors.md
index 918cfa343136af..1e091b46a1d8d3 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -1353,7 +1353,14 @@ An attempt was made to `require()` an [ES6 module][].
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.
+and HTTP/2 `Server` instances.
+
+
+### 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,
+and HTTP/2 `Server` instances.
### ERR_SOCKET_ALREADY_BOUND
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 7276bb6344a5bd..72db142c7a0e81 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -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.');
diff --git a/lib/net.js b/lib/net.js
index 245415d87c63a1..bfafbcc21915ba 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -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;
}
@@ -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);
diff --git a/test/parallel/test-http-unix-socket.js b/test/parallel/test-http-unix-socket.js
index 6d5897cacbecc2..7a17b9bc9ca50a 100644
--- a/test/parallel/test-http-unix-socket.js
+++ b/test/parallel/test-http-unix-socket.js
@@ -58,8 +58,10 @@ server.listen(common.PIPE, common.mustCall(function() {
assert.strictEqual(res.body, 'hello world\n');
server.close(common.mustCall(function(error) {
assert.strictEqual(error, undefined);
- server.close(common.mustCall(function(error) {
- assert.strictEqual(error && error.message, 'Not running');
+ server.close(common.expectsError({
+ code: 'ERR_SERVER_NOT_RUNNING',
+ message: 'Server is not running.',
+ type: Error
}));
}));
}));
diff --git a/test/parallel/test-net-socket-destroy-send.js b/test/parallel/test-net-socket-destroy-send.js
index 6dc4b9566a3900..a602b89253887d 100644
--- a/test/parallel/test-net-socket-destroy-send.js
+++ b/test/parallel/test-net-socket-destroy-send.js
@@ -12,11 +12,16 @@ server.listen(0, common.mustCall(function() {
conn.on('connect', 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) {
- assert.strictEqual(err.message, 'This socket is closed');
+ conn.on('error', common.expectsError({
+ code: 'ERR_SOCKET_CLOSED',
+ message: 'Socket is closed',
+ type: Error
}));
- conn.write(Buffer.from('kaboom'), common.mustCall(function(err) {
- assert.strictEqual(err.message, 'This socket is closed');
+
+ conn.write(Buffer.from('kaboom'), common.expectsError({
+ code: 'ERR_SOCKET_CLOSED',
+ message: 'Socket is closed',
+ type: Error
}));
server.close();
}));
diff --git a/test/parallel/test-net-socket-write-after-close.js b/test/parallel/test-net-socket-write-after-close.js
index d3a3d937d338ab..e3593a3a09a146 100644
--- a/test/parallel/test-net-socket-write-after-close.js
+++ b/test/parallel/test-net-socket-write-after-close.js
@@ -26,11 +26,14 @@ const net = require('net');
server.listen(common.mustCall(() => {
const port = server.address().port;
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');
+ client.on('error', common.expectsError({
+ code: 'ERR_SOCKET_CLOSED',
+ message: 'Socket is closed',
+ type: Error
}));
+
+ server.close();
+
client._handle.close();
client._handle = null;
client.write('foo');