Skip to content

Commit fe730d3

Browse files
committed
child_process: use internal/errors
PR-URL: #14009 Refs: #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>
1 parent 44256bb commit fe730d3

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

‎lib/internal/child_process.js‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ ChildProcess.prototype.spawn = function(options) {
264264
varipcFd;
265265
vari;
266266

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

270272
// If no `stdio` option was given - use default
271273
varstdio=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-
elseif(!Array.isArray(options.envPairs))
284-
thrownewTypeError('"envPairs" must be an array');
285+
elseif(!Array.isArray(options.envPairs)){
286+
thrownewerrors.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(typeofoptions.file==='string')
290-
this.spawnfile=options.file;
291-
else
292-
thrownewTypeError('"file" must be a string');
293+
if(typeofoptions.file!=='string'){
294+
thrownewerrors.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
elseif(options.args===undefined)
297302
this.spawnargs=[];
298303
else
299-
thrownewTypeError('"args" must be an array');
304+
thrownewerrors.TypeError('ERR_INVALID_ARG_TYPE','options.args','array',
305+
options.args);
300306

301307
varerr=this._handle.spawn(options);
302308

@@ -574,7 +580,8 @@ function setupChannel(target, channel) {
574580
options=undefined;
575581
}elseif(options!==undefined&&
576582
(options===null||typeofoptions!=='object')){
577-
thrownewerrors.TypeError('ERR_INVALID_ARG_TYPE','options','Object');
583+
thrownewerrors.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‎

Lines changed: 28 additions & 8 deletions
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(typeofChildProcess,'function');
77

8+
functiontypeName(value){
9+
returnvalue===null ? 'null' : typeofvalue;
10+
}
11+
812
{
913
// Verify that invalid options to spawn() throw.
1014
constchild=newChildProcess();
11-
constre=/^TypeError:"options"mustbeanobject$/;
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
constchild=newChildProcess();
23-
constre=/^TypeError:"file"mustbeastring$/;
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
constchild=newChildProcess();
35-
constre=/^TypeError:"envPairs"mustbeanarray$/;
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
constchild=newChildProcess();
47-
constre=/^TypeError:"args"mustbeanarray$/;
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)