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: https://github.com/nodejs/node/pull/17766
Refs: https://github.com/nodejs/node/issues/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>
This commit is contained in:
kysnm 2017-12-20 08:07:41 +09:00 коммит произвёл Joyee Cheung
Родитель 8599465d33
Коммит b98aaa312e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: F586868AAD831D0C
6 изменённых файлов: 31 добавлений и 13 удалений

Просмотреть файл

@ -1364,7 +1364,14 @@ Script execution was interrupted by `SIGINT` (For example, when Ctrl+C was press
The [`server.listen()`][] method was called while a `net.Server` was already The [`server.listen()`][] method was called while a `net.Server` was already
listening. This applies to all instances of `net.Server`, including HTTP, HTTPS, listening. This applies to all instances of `net.Server`, including HTTP, HTTPS,
and HTTP/2 Server instances. 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,
and HTTP/2 `Server` instances.
<a id="ERR_SOCKET_ALREADY_BOUND"></a> <a id="ERR_SOCKET_ALREADY_BOUND"></a>
### ERR_SOCKET_ALREADY_BOUND ### ERR_SOCKET_ALREADY_BOUND

Просмотреть файл

@ -451,6 +451,7 @@ E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
'Script execution was interrupted by `SIGINT`.'); 'Script execution was interrupted by `SIGINT`.');
E('ERR_SERVER_ALREADY_LISTEN', E('ERR_SERVER_ALREADY_LISTEN',
'Listen method has been called more than once without closing.'); '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_ALREADY_BOUND', 'Socket is already bound');
E('ERR_SOCKET_BAD_BUFFER_SIZE', 'Buffer size must be a positive integer'); 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.'); E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536. Received %s.');

Просмотреть файл

@ -744,7 +744,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
this._unrefTimer(); this._unrefTimer();
if (!this._handle) { if (!this._handle) {
this.destroy(new Error('This socket is closed'), cb); this.destroy(new errors.Error('ERR_SOCKET_CLOSED'), cb);
return false; return false;
} }
@ -1645,7 +1645,7 @@ Server.prototype.close = function(cb) {
if (typeof cb === 'function') { if (typeof cb === 'function') {
if (!this._handle) { if (!this._handle) {
this.once('close', function close() { this.once('close', function close() {
cb(new Error('Not running')); cb(new errors.Error('ERR_SERVER_NOT_RUNNING'));
}); });
} else { } else {
this.once('close', cb); this.once('close', cb);

Просмотреть файл

@ -58,8 +58,10 @@ server.listen(common.PIPE, common.mustCall(function() {
assert.strictEqual(res.body, 'hello world\n'); assert.strictEqual(res.body, 'hello world\n');
server.close(common.mustCall(function(error) { server.close(common.mustCall(function(error) {
assert.strictEqual(error, undefined); assert.strictEqual(error, undefined);
server.close(common.mustCall(function(error) { server.close(common.expectsError({
assert.strictEqual(error && error.message, 'Not running'); code: 'ERR_SERVER_NOT_RUNNING',
message: 'Server is not running.',
type: Error
})); }));
})); }));
})); }));

Просмотреть файл

@ -12,11 +12,16 @@ server.listen(0, common.mustCall(function() {
conn.on('connect', common.mustCall(function() { conn.on('connect', common.mustCall(function() {
// Test destroy returns this, even on multiple calls when it short-circuits. // Test destroy returns this, even on multiple calls when it short-circuits.
assert.strictEqual(conn, conn.destroy().destroy()); assert.strictEqual(conn, conn.destroy().destroy());
conn.on('error', common.mustCall(function(err) { conn.on('error', common.expectsError({
assert.strictEqual(err.message, 'This socket is closed'); 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(); server.close();
})); }));

Просмотреть файл

@ -26,11 +26,14 @@ const net = require('net');
server.listen(common.mustCall(() => { server.listen(common.mustCall(() => {
const port = server.address().port; const port = server.address().port;
const client = net.connect({ port }, common.mustCall(() => { const client = net.connect({ port }, common.mustCall(() => {
client.on('error', common.mustCall((err) => { client.on('error', common.expectsError({
server.close(); code: 'ERR_SOCKET_CLOSED',
assert.strictEqual(err.constructor, Error); message: 'Socket is closed',
assert.strictEqual(err.message, 'This socket is closed'); type: Error
})); }));
server.close();
client._handle.close(); client._handle.close();
client._handle = null; client._handle = null;
client.write('foo'); client.write('foo');