child_process: use internal/errors

PR-URL: https://github.com/nodejs/node/pull/14009
Refs: https://github.com/nodejs/node/issues/11273
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Tobias Nießen 2017-06-30 20:46:11 +02:00
parent 44256bb0aa
commit fe730d34ce
2 changed files with 45 additions and 18 deletions

View File

@ -264,8 +264,10 @@ ChildProcess.prototype.spawn = function(options) {
var ipcFd; var ipcFd;
var i; var i;
if (options === null || typeof options !== 'object') if (options === null || typeof options !== 'object') {
throw new TypeError('"options" must be an object'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object',
options);
}
// If no `stdio` option was given - use default // If no `stdio` option was given - use default
var stdio = options.stdio || 'pipe'; var stdio = options.stdio || 'pipe';
@ -280,23 +282,27 @@ ChildProcess.prototype.spawn = function(options) {
// Let child process know about opened IPC channel // Let child process know about opened IPC channel
if (options.envPairs === undefined) if (options.envPairs === undefined)
options.envPairs = []; options.envPairs = [];
else if (!Array.isArray(options.envPairs)) else if (!Array.isArray(options.envPairs)) {
throw new TypeError('"envPairs" must be an array'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.envPairs',
'array', options.envPairs);
}
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd); options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
} }
if (typeof options.file === 'string') if (typeof options.file !== 'string') {
this.spawnfile = options.file; throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.file', 'string',
else options.file);
throw new TypeError('"file" must be a string'); }
this.spawnfile = options.file;
if (Array.isArray(options.args)) if (Array.isArray(options.args))
this.spawnargs = options.args; this.spawnargs = options.args;
else if (options.args === undefined) else if (options.args === undefined)
this.spawnargs = []; this.spawnargs = [];
else else
throw new TypeError('"args" must be an array'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'array',
options.args);
var err = this._handle.spawn(options); var err = this._handle.spawn(options);
@ -574,7 +580,8 @@ function setupChannel(target, channel) {
options = undefined; options = undefined;
} else if (options !== undefined && } else if (options !== undefined &&
(options === null || typeof options !== 'object')) { (options === null || typeof options !== 'object')) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object',
options);
} }
options = Object.assign({swallowErrors: false}, options); options = Object.assign({swallowErrors: false}, options);

View File

@ -5,51 +5,71 @@ const assert = require('assert');
const { ChildProcess } = require('child_process'); const { ChildProcess } = require('child_process');
assert.strictEqual(typeof ChildProcess, 'function'); assert.strictEqual(typeof ChildProcess, 'function');
function typeName(value) {
return value === null ? 'null' : typeof value;
}
{ {
// Verify that invalid options to spawn() throw. // Verify that invalid options to spawn() throw.
const child = new ChildProcess(); const child = new ChildProcess();
const re = /^TypeError: "options" must be an object$/;
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => { [undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
assert.throws(() => { assert.throws(() => {
child.spawn(options); child.spawn(options);
}, re); }, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type object. Received type ' +
typeName(options)
}));
}); });
} }
{ {
// Verify that spawn throws if file is not a string. // Verify that spawn throws if file is not a string.
const child = new ChildProcess(); const child = new ChildProcess();
const re = /^TypeError: "file" must be a string$/;
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => { [undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
assert.throws(() => { assert.throws(() => {
child.spawn({ file }); child.spawn({ file });
}, re); }, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.file" property must be of type string. Received ' +
'type ' + typeName(file)
}));
}); });
} }
{ {
// Verify that spawn throws if envPairs is not an array or undefined. // Verify that spawn throws if envPairs is not an array or undefined.
const child = new ChildProcess(); const child = new ChildProcess();
const re = /^TypeError: "envPairs" must be an array$/;
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => { [null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
assert.throws(() => { assert.throws(() => {
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] }); child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
}, re); }, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.envPairs" property must be of type array. ' +
'Received type ' + typeName(envPairs)
}));
}); });
} }
{ {
// Verify that spawn throws if args is not an array or undefined. // Verify that spawn throws if args is not an array or undefined.
const child = new ChildProcess(); const child = new ChildProcess();
const re = /^TypeError: "args" must be an array$/;
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => { [null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
assert.throws(() => { assert.throws(() => {
child.spawn({ file: 'foo', args }); child.spawn({ file: 'foo', args });
}, re); }, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.args" property must be of type array. Received ' +
'type ' + typeName(args)
}));
}); });
} }