Skip to content

Commit

Permalink
errors: update internal/child_process.js to use internal/errors
Browse files Browse the repository at this point in the history
Update most of the errors to use internal/errors. Purposefully leaves
two errors in place to be updated later.
  • Loading branch information
jasnell committed Oct 25, 2016
1 parent 5866425 commit 4879291
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
27 changes: 14 additions & 13 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const dgram = require('dgram');
const util = require('util');
const constants = process.binding('constants').os.signals;
const assert = require('assert');
const errors = require('internal/errors');

const Process = process.binding('process_wrap').Process;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
Expand Down Expand Up @@ -372,7 +373,7 @@ ChildProcess.prototype.kill = function(sig) {
}

if (signal === undefined) {
throw new Error('Unknown signal: ' + sig);
throw new errors.Error('INVALID_SIGNAL', sig);
}

if (this._handle) {
Expand Down Expand Up @@ -537,7 +538,7 @@ function setupChannel(target, channel) {
if (this.connected) {
return this._send(message, handle, options, callback);
}
const ex = new Error('channel closed');
const ex = new errors.Error('CHANNEL_CLOSED');
if (typeof callback === 'function') {
process.nextTick(callback, ex);
} else {
Expand Down Expand Up @@ -577,7 +578,7 @@ function setupChannel(target, channel) {
} else if (handle instanceof UDP) {
message.type = 'dgram.Native';
} else {
throw new TypeError('This handle type can\'t be sent');
throw new errors.TypeError('INVALID_HANDLE_TYPE');
}

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

target.disconnect = function() {
if (!this.connected) {
this.emit('error', new Error('IPC channel is already disconnected'));
this.emit('error', new errors.Error('CHILD_PROCESS_IPC_DISCONNECTED'));
return;
}

Expand Down Expand Up @@ -757,11 +758,11 @@ function _validateStdio(stdio, sync) {
case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
case 'inherit': stdio = [0, 1, 2]; break;
default: throw new TypeError('Incorrect value of stdio option: ' + stdio);
default: throw new errors.TypeError('CHILD_PROCESS_INVALID_STDIO', stdio);
}
} else if (!Array.isArray(stdio)) {
throw new TypeError('Incorrect value of stdio option: ' +
util.inspect(stdio));
throw new errors.TypeError('CHILD_PROCESS_INVALID_STDIO',
util.inspect(stdio));
}

// At least 3 stdio will be created
Expand Down Expand Up @@ -805,9 +806,9 @@ function _validateStdio(stdio, sync) {
// Cleanup previously created pipes
cleanup();
if (!sync)
throw new Error('Child process can have only one IPC pipe');
throw new errors.Error('CHILD_PROCESS_ONE_IPC');
else
throw new Error('You cannot use IPC with synchronous forks');
throw new errors.Error('CHILD_PROCESS_SYNCHRONOUS_FORK_NO_IPC');
}

ipc = new Pipe(true);
Expand Down Expand Up @@ -842,14 +843,14 @@ function _validateStdio(stdio, sync) {
} else if (stdio instanceof Buffer || typeof stdio === 'string') {
if (!sync) {
cleanup();
throw new TypeError('Asynchronous forks do not support Buffer input: ' +
util.inspect(stdio));
throw new errors.TypeError('CHILD_PROCESS_ASYNC_NO_BUFFER',
util.inspect(stdio));
}
} else {
// Cleanup
cleanup();
throw new TypeError('Incorrect value for stdio stream: ' +
util.inspect(stdio));
throw new errors.TypeError('CHILD_PROCESS_INVALID_STDIO',
util.inspect(stdio));
}

return acc;
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,21 @@ function E(sym, val) {

// Note: Please try to keep these in alphabetical order
E('ASSERTION_ERROR', (msg) => msg);
E('CHANNEL_CLOSED', 'channel closed');
E('CHILD_PROCESS_ASYNC_NO_BUFFER',
(value) => `Asynchronous forks do not support Buffer input: ${value}`);
E('CHILD_PROCESS_INVALID_STDIO',
(value) => `Incorrect value of stdio option: ${value}`);
E('CHILD_PROCESS_IPC_DISCONNECTED', 'IPC channel is already disconnected');
E('CHILD_PROCESS_ONE_IPC', 'Child process can have only one IPC pipe');
E('CHILD_PROCESS_SYNCHRONOUS_FORK_NO_IPC',
'IPC cannot be used with synchronous forks');
E('INVALID_CALLBACK', 'callback is not a function');
E('INVALID_ENCODING', (encoding) => `Unknown encoding: ${encoding}`);
E('INVALID_FILE_OPEN_FLAG', (flag) => `Unknown file open flag: ${flag}`);
E('INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
E('INVALID_PORT', 'port must be a number >= 0 and <= 65535');
E('INVALID_SIGNAL', (signal) => `Unknown signal: ${signal}`);
E('INVALID_URL', 'Invalid URL');
E('NATIVE_MODULE_NOT_FOUND', (module) => `No such native module ${module}`);
E('NO_CRYPTO', 'Node.js is not compiled with openssl crypto support');
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-child-process-stdio.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const errors = require('internal/errors');

const one_ipc_err = new RegExp(errors.message('CHILD_PROCESS_ONE_IPC'));

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

assert.throws(() => {
common.spawnPwd({stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'ipc']});
}, /^Error: Child process can have only one IPC pipe$/);
}, one_ipc_err);
8 changes: 5 additions & 3 deletions test/parallel/test-child-process-validate-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
require('../common');
const assert = require('assert');
const _validateStdio = require('internal/child_process')._validateStdio;
const errors = require('internal/errors');

const no_ipc_err =
new RegExp(errors.message('CHILD_PROCESS_SYNCHRONOUS_FORK_NO_IPC'));

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

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

{
const stdio3 = [process.stdin, process.stdout, process.stderr];
Expand Down

0 comments on commit 4879291

Please sign in to comment.