Skip to content

Commit 4879291

Browse files
committed
errors: update internal/child_process.js to use internal/errors
Update most of the errors to use internal/errors. Purposefully leaves two errors in place to be updated later.
1 parent 5866425 commit 4879291

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

‎lib/internal/child_process.js‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const dgram = require('dgram');
88
const util = require('util');
99
const constants = process.binding('constants').os.signals;
1010
const assert = require('assert');
11+
const errors = require('internal/errors');
1112

1213
const Process = process.binding('process_wrap').Process;
1314
const WriteWrap = process.binding('stream_wrap').WriteWrap;
@@ -372,7 +373,7 @@ ChildProcess.prototype.kill = function(sig) {
372373
}
373374

374375
if (signal === undefined) {
375-
throw new Error('Unknown signal: ' + sig);
376+
throw new errors.Error('INVALID_SIGNAL', sig);
376377
}
377378

378379
if (this._handle) {
@@ -537,7 +538,7 @@ function setupChannel(target, channel) {
537538
if (this.connected) {
538539
return this._send(message, handle, options, callback);
539540
}
540-
const ex = new Error('channel closed');
541+
const ex = new errors.Error('CHANNEL_CLOSED');
541542
if (typeof callback === 'function') {
542543
process.nextTick(callback, ex);
543544
} else {
@@ -577,7 +578,7 @@ function setupChannel(target, channel) {
577578
} else if (handle instanceof UDP) {
578579
message.type = 'dgram.Native';
579580
} else {
580-
throw new TypeError('This handle type can\'t be sent');
581+
throw new errors.TypeError('INVALID_HANDLE_TYPE');
581582
}
582583

583584
// Queue-up message and handle if we haven't received ACK yet.
@@ -677,7 +678,7 @@ function setupChannel(target, channel) {
677678

678679
target.disconnect = function() {
679680
if (!this.connected) {
680-
this.emit('error', new Error('IPC channel is already disconnected'));
681+
this.emit('error', new errors.Error('CHILD_PROCESS_IPC_DISCONNECTED'));
681682
return;
682683
}
683684

@@ -757,11 +758,11 @@ function _validateStdio(stdio, sync) {
757758
case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
758759
case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
759760
case 'inherit': stdio = [0, 1, 2]; break;
760-
default: throw new TypeError('Incorrect value of stdio option: ' + stdio);
761+
default: throw new errors.TypeError('CHILD_PROCESS_INVALID_STDIO', stdio);
761762
}
762763
} else if (!Array.isArray(stdio)) {
763-
throw new TypeError('Incorrect value of stdio option: ' +
764-
util.inspect(stdio));
764+
throw new errors.TypeError('CHILD_PROCESS_INVALID_STDIO',
765+
util.inspect(stdio));
765766
}
766767

767768
// At least 3 stdio will be created
@@ -805,9 +806,9 @@ function _validateStdio(stdio, sync) {
805806
// Cleanup previously created pipes
806807
cleanup();
807808
if (!sync)
808-
throw new Error('Child process can have only one IPC pipe');
809+
throw new errors.Error('CHILD_PROCESS_ONE_IPC');
809810
else
810-
throw new Error('You cannot use IPC with synchronous forks');
811+
throw new errors.Error('CHILD_PROCESS_SYNCHRONOUS_FORK_NO_IPC');
811812
}
812813

813814
ipc = new Pipe(true);
@@ -842,14 +843,14 @@ function _validateStdio(stdio, sync) {
842843
} else if (stdio instanceof Buffer || typeof stdio === 'string') {
843844
if (!sync) {
844845
cleanup();
845-
throw new TypeError('Asynchronous forks do not support Buffer input: ' +
846-
util.inspect(stdio));
846+
throw new errors.TypeError('CHILD_PROCESS_ASYNC_NO_BUFFER',
847+
util.inspect(stdio));
847848
}
848849
} else {
849850
// Cleanup
850851
cleanup();
851-
throw new TypeError('Incorrect value for stdio stream: ' +
852-
util.inspect(stdio));
852+
throw new errors.TypeError('CHILD_PROCESS_INVALID_STDIO',
853+
util.inspect(stdio));
853854
}
854855

855856
return acc;

‎lib/internal/errors.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,21 @@ function E(sym, val) {
118118

119119
// Note: Please try to keep these in alphabetical order
120120
E('ASSERTION_ERROR', (msg) => msg);
121+
E('CHANNEL_CLOSED', 'channel closed');
122+
E('CHILD_PROCESS_ASYNC_NO_BUFFER',
123+
(value) => `Asynchronous forks do not support Buffer input: ${value}`);
124+
E('CHILD_PROCESS_INVALID_STDIO',
125+
(value) => `Incorrect value of stdio option: ${value}`);
126+
E('CHILD_PROCESS_IPC_DISCONNECTED', 'IPC channel is already disconnected');
127+
E('CHILD_PROCESS_ONE_IPC', 'Child process can have only one IPC pipe');
128+
E('CHILD_PROCESS_SYNCHRONOUS_FORK_NO_IPC',
129+
'IPC cannot be used with synchronous forks');
121130
E('INVALID_CALLBACK', 'callback is not a function');
122131
E('INVALID_ENCODING', (encoding) => `Unknown encoding: ${encoding}`);
123132
E('INVALID_FILE_OPEN_FLAG', (flag) => `Unknown file open flag: ${flag}`);
133+
E('INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
124134
E('INVALID_PORT', 'port must be a number >= 0 and <= 65535');
135+
E('INVALID_SIGNAL', (signal) => `Unknown signal: ${signal}`);
125136
E('INVALID_URL', 'Invalid URL');
126137
E('NATIVE_MODULE_NOT_FOUND', (module) => `No such native module ${module}`);
127138
E('NO_CRYPTO', 'Node.js is not compiled with openssl crypto support');

‎test/parallel/test-child-process-stdio.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
// Flags: --expose-internals
12
'use strict';
23
const common = require('../common');
34
const assert = require('assert');
5+
const errors = require('internal/errors');
6+
7+
const one_ipc_err = new RegExp(errors.message('CHILD_PROCESS_ONE_IPC'));
48

59
let options = {stdio: ['pipe']};
610
let child = common.spawnPwd(options);
@@ -20,4 +24,4 @@ assert.deepStrictEqual(options, {stdio: 'ignore'});
2024

2125
assert.throws(() => {
2226
common.spawnPwd({stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'ipc']});
23-
}, /^Error: Child process can have only one IPC pipe$/);
27+
}, one_ipc_err);

‎test/parallel/test-child-process-validate-stdio.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
require('../common');
55
const assert = require('assert');
66
const _validateStdio = require('internal/child_process')._validateStdio;
7+
const errors = require('internal/errors');
8+
9+
const no_ipc_err =
10+
new RegExp(errors.message('CHILD_PROCESS_SYNCHRONOUS_FORK_NO_IPC'));
711

812
// should throw if string and not ignore, pipe, or inherit
913
assert.throws(function() {
@@ -27,9 +31,7 @@ assert.throws(function() {
2731

2832
// should throw if stdio has ipc and sync is true
2933
const stdio2 = ['ipc', 'ipc', 'ipc'];
30-
assert.throws(function() {
31-
_validateStdio(stdio2, true);
32-
}, /You cannot use IPC with synchronous forks/);
34+
assert.throws(() => _validateStdio(stdio2, true), no_ipc_err);
3335

3436
{
3537
const stdio3 = [process.stdin, process.stdout, process.stderr];

0 commit comments

Comments
 (0)