Skip to content

Commit fe730d3

Browse files
committed
child_process: use internal/errors
PR-URL: #14009 Refs: #11273 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 44256bb commit fe730d3

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

lib/internal/child_process.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ ChildProcess.prototype.spawn = function(options) {
264264
var ipcFd;
265265
var i;
266266

267-
if (options === null || typeof options !== 'object')
268-
throw new TypeError('"options" must be an object');
267+
if (options === null || typeof options !== 'object') {
268+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object',
269+
options);
270+
}
269271

270272
// If no `stdio` option was given - use default
271273
var stdio = options.stdio || 'pipe';
@@ -280,23 +282,27 @@ ChildProcess.prototype.spawn = function(options) {
280282
// Let child process know about opened IPC channel
281283
if (options.envPairs === undefined)
282284
options.envPairs = [];
283-
else if (!Array.isArray(options.envPairs))
284-
throw new TypeError('"envPairs" must be an array');
285+
else if (!Array.isArray(options.envPairs)) {
286+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.envPairs',
287+
'array', options.envPairs);
288+
}
285289

286290
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
287291
}
288292

289-
if (typeof options.file === 'string')
290-
this.spawnfile = options.file;
291-
else
292-
throw new TypeError('"file" must be a string');
293+
if (typeof options.file !== 'string') {
294+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.file', 'string',
295+
options.file);
296+
}
297+
this.spawnfile = options.file;
293298

294299
if (Array.isArray(options.args))
295300
this.spawnargs = options.args;
296301
else if (options.args === undefined)
297302
this.spawnargs = [];
298303
else
299-
throw new TypeError('"args" must be an array');
304+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'array',
305+
options.args);
300306

301307
var err = this._handle.spawn(options);
302308

@@ -574,7 +580,8 @@ function setupChannel(target, channel) {
574580
options = undefined;
575581
} else if (options !== undefined &&
576582
(options === null || typeof options !== 'object')) {
577-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
583+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object',
584+
options);
578585
}
579586

580587
options = Object.assign({swallowErrors: false}, options);

test/parallel/test-child-process-constructor.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,71 @@ const assert = require('assert');
55
const { ChildProcess } = require('child_process');
66
assert.strictEqual(typeof ChildProcess, 'function');
77

8+
function typeName(value) {
9+
return value === null ? 'null' : typeof value;
10+
}
11+
812
{
913
// Verify that invalid options to spawn() throw.
1014
const child = new ChildProcess();
11-
const re = /^TypeError: "options" must be an object$/;
1215

1316
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
1417
assert.throws(() => {
1518
child.spawn(options);
16-
}, re);
19+
}, common.expectsError({
20+
code: 'ERR_INVALID_ARG_TYPE',
21+
type: TypeError,
22+
message: 'The "options" argument must be of type object. Received type ' +
23+
typeName(options)
24+
}));
1725
});
1826
}
1927

2028
{
2129
// Verify that spawn throws if file is not a string.
2230
const child = new ChildProcess();
23-
const re = /^TypeError: "file" must be a string$/;
2431

2532
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
2633
assert.throws(() => {
2734
child.spawn({ file });
28-
}, re);
35+
}, common.expectsError({
36+
code: 'ERR_INVALID_ARG_TYPE',
37+
type: TypeError,
38+
message: 'The "options.file" property must be of type string. Received ' +
39+
'type ' + typeName(file)
40+
}));
2941
});
3042
}
3143

3244
{
3345
// Verify that spawn throws if envPairs is not an array or undefined.
3446
const child = new ChildProcess();
35-
const re = /^TypeError: "envPairs" must be an array$/;
3647

3748
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
3849
assert.throws(() => {
3950
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
40-
}, re);
51+
}, common.expectsError({
52+
code: 'ERR_INVALID_ARG_TYPE',
53+
type: TypeError,
54+
message: 'The "options.envPairs" property must be of type array. ' +
55+
'Received type ' + typeName(envPairs)
56+
}));
4157
});
4258
}
4359

4460
{
4561
// Verify that spawn throws if args is not an array or undefined.
4662
const child = new ChildProcess();
47-
const re = /^TypeError: "args" must be an array$/;
4863

4964
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
5065
assert.throws(() => {
5166
child.spawn({ file: 'foo', args });
52-
}, re);
67+
}, common.expectsError({
68+
code: 'ERR_INVALID_ARG_TYPE',
69+
type: TypeError,
70+
message: 'The "options.args" property must be of type array. Received ' +
71+
'type ' + typeName(args)
72+
}));
5373
});
5474
}
5575

0 commit comments

Comments
 (0)