@@ -29,6 +29,7 @@ const { createPromise,
29
29
const debug = util . debuglog ( 'child_process' ) ;
30
30
const { Buffer } = require ( 'buffer' ) ;
31
31
const { Pipe, constants : PipeConstants } = process . binding ( 'pipe_wrap' ) ;
32
+ const errors = require ( 'internal/errors' ) ;
32
33
const { errname } = process . binding ( 'uv' ) ;
33
34
const child_process = require ( 'internal/child_process' ) ;
34
35
const {
@@ -46,7 +47,7 @@ function stdioStringToArray(option) {
46
47
case 'inherit' :
47
48
return [ option , option , option , 'ipc' ] ;
48
49
default :
49
- throw new TypeError ( 'Incorrect value of stdio option: ' + option ) ;
50
+ throw new errors . TypeError ( 'ERR_INVALID_OPT_VALUE' , ' stdio' , option ) ;
50
51
}
51
52
}
52
53
@@ -63,7 +64,9 @@ exports.fork = function(modulePath /*, args, options*/) {
63
64
64
65
if ( pos < arguments . length && arguments [ pos ] != null ) {
65
66
if ( typeof arguments [ pos ] !== 'object' ) {
66
- throw new TypeError ( 'Incorrect value of args option' ) ;
67
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_VALUE' ,
68
+ `arguments[${ pos } ]` ,
69
+ arguments [ pos ] ) ;
67
70
}
68
71
69
72
options = util . _extend ( { } , arguments [ pos ++ ] ) ;
@@ -91,7 +94,8 @@ exports.fork = function(modulePath /*, args, options*/) {
91
94
options . stdio = options . silent ? stdioStringToArray ( 'pipe' ) :
92
95
stdioStringToArray ( 'inherit' ) ;
93
96
} else if ( options . stdio . indexOf ( 'ipc' ) === - 1 ) {
94
- throw new TypeError ( 'Forked processes must have an IPC channel' ) ;
97
+ throw new errors . Error ( 'ERR_CHILD_PROCESS_IPC_REQUIRED' ,
98
+ 'options.stdio' ) ;
95
99
}
96
100
97
101
options . execPath = options . execPath || process . execPath ;
@@ -195,7 +199,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
195
199
}
196
200
197
201
if ( ! callback && pos < arguments . length && arguments [ pos ] != null ) {
198
- throw new TypeError ( 'Incorrect value of args option' ) ;
202
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_VALUE' , ' args' , arguments [ pos ] ) ;
199
203
}
200
204
201
205
// Validate the timeout, if present.
@@ -322,7 +326,8 @@ exports.execFile = function(file /*, args, options, callback*/) {
322
326
stdoutLen += encoding ? Buffer . byteLength ( chunk , encoding ) : chunk . length ;
323
327
324
328
if ( stdoutLen > options . maxBuffer ) {
325
- ex = new Error ( 'stdout maxBuffer exceeded' ) ;
329
+ ex = new errors . RangeError ( 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ,
330
+ 'stdout' ) ;
326
331
kill ( ) ;
327
332
} else if ( encoding ) {
328
333
_stdout += chunk ;
@@ -340,7 +345,8 @@ exports.execFile = function(file /*, args, options, callback*/) {
340
345
stderrLen += encoding ? Buffer . byteLength ( chunk , encoding ) : chunk . length ;
341
346
342
347
if ( stderrLen > options . maxBuffer ) {
343
- ex = new Error ( 'stderr maxBuffer exceeded' ) ;
348
+ ex = new errors . RangeError ( 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ,
349
+ 'stderr' ) ;
344
350
kill ( ) ;
345
351
} else if ( encoding ) {
346
352
_stderr += chunk ;
@@ -377,13 +383,13 @@ function _convertCustomFds(options) {
377
383
378
384
function normalizeSpawnArguments ( file , args , options ) {
379
385
if ( typeof file !== 'string' || file . length === 0 )
380
- throw new TypeError ( '" file" argument must be a non-empty string' ) ;
386
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , ' file' , ' string', file ) ;
381
387
382
388
if ( Array . isArray ( args ) ) {
383
389
args = args . slice ( 0 ) ;
384
390
} else if ( args !== undefined &&
385
391
( args === null || typeof args !== 'object' ) ) {
386
- throw new TypeError ( 'Incorrect value of args option' ) ;
392
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , ' args' , 'object' , args ) ;
387
393
} else {
388
394
options = args ;
389
395
args = [ ] ;
@@ -392,41 +398,62 @@ function normalizeSpawnArguments(file, args, options) {
392
398
if ( options === undefined )
393
399
options = { } ;
394
400
else if ( options === null || typeof options !== 'object' )
395
- throw new TypeError ( '"options" argument must be an object' ) ;
401
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
402
+ 'options' ,
403
+ 'object' ,
404
+ options ) ;
396
405
397
406
// Validate the cwd, if present.
398
407
if ( options . cwd != null &&
399
408
typeof options . cwd !== 'string' ) {
400
- throw new TypeError ( '"cwd" must be a string' ) ;
409
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
410
+ 'options.cwd' ,
411
+ 'string' ,
412
+ options . cwd ) ;
401
413
}
402
414
403
415
// Validate detached, if present.
404
416
if ( options . detached != null &&
405
417
typeof options . detached !== 'boolean' ) {
406
- throw new TypeError ( '"detached" must be a boolean' ) ;
418
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
419
+ 'options.detached' ,
420
+ 'boolean' ,
421
+ options . detached ) ;
407
422
}
408
423
409
424
// Validate the uid, if present.
410
425
if ( options . uid != null && ! Number . isInteger ( options . uid ) ) {
411
- throw new TypeError ( '"uid" must be an integer' ) ;
426
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
427
+ 'options.uid' ,
428
+ 'integer' ,
429
+ options . uid ) ;
412
430
}
413
431
414
432
// Validate the gid, if present.
415
433
if ( options . gid != null && ! Number . isInteger ( options . gid ) ) {
416
- throw new TypeError ( '"gid" must be an integer' ) ;
434
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
435
+ 'options.gid' ,
436
+ 'integer' ,
437
+ options . gid ) ;
417
438
}
418
439
419
440
// Validate the shell, if present.
420
441
if ( options . shell != null &&
421
442
typeof options . shell !== 'boolean' &&
422
443
typeof options . shell !== 'string' ) {
423
- throw new TypeError ( '"shell" must be a boolean or string' ) ;
444
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
445
+ 'options.shell' ,
446
+ [ 'boolean' , 'string' ] ,
447
+ options . shell ) ;
424
448
}
425
449
426
450
// Validate argv0, if present.
427
451
if ( options . argv0 != null &&
428
452
typeof options . argv0 !== 'string' ) {
429
- throw new TypeError ( '"argv0" must be a string' ) ;
453
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
454
+ 'options.argv0' ,
455
+ 'string' ,
456
+ options . argv0 ) ;
430
457
}
431
458
432
459
// Validate windowsHide, if present.
@@ -438,7 +465,10 @@ function normalizeSpawnArguments(file, args, options) {
438
465
// Validate windowsVerbatimArguments, if present.
439
466
if ( options . windowsVerbatimArguments != null &&
440
467
typeof options . windowsVerbatimArguments !== 'boolean' ) {
441
- throw new TypeError ( '"windowsVerbatimArguments" must be a boolean' ) ;
468
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
469
+ 'options.windowsVerbatimArguments' ,
470
+ 'boolean' ,
471
+ options . windowsVerbatimArguments ) ;
442
472
}
443
473
444
474
// Make a shallow copy so we don't clobber the user's options object.
@@ -549,10 +579,10 @@ function spawnSync(/*file, args, options*/) {
549
579
} else if ( typeof input === 'string' ) {
550
580
pipe . input = Buffer . from ( input , options . encoding ) ;
551
581
} else {
552
- throw new TypeError ( util . format (
553
- 'stdio[%d] should be Buffer, Uint8Array or string not %s' ,
554
- i ,
555
- typeof input ) ) ;
582
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
583
+ ( 'options.stdio[' + i + ']' ) ,
584
+ [ 'Buffer' , 'Uint8Array' , 'string' ] ,
585
+ input ) ;
556
586
}
557
587
}
558
588
}
@@ -620,14 +650,20 @@ exports.execSync = execSync;
620
650
621
651
function validateTimeout ( timeout ) {
622
652
if ( timeout != null && ! ( Number . isInteger ( timeout ) && timeout >= 0 ) ) {
623
- throw new TypeError ( '"timeout" must be an unsigned integer' ) ;
653
+ throw new errors . RangeError ( 'ERR_VALUE_OUT_OF_RANGE' ,
654
+ 'timeout' ,
655
+ 'an unsigned integer' ,
656
+ timeout ) ;
624
657
}
625
658
}
626
659
627
660
628
661
function validateMaxBuffer ( maxBuffer ) {
629
662
if ( maxBuffer != null && ! ( typeof maxBuffer === 'number' && maxBuffer >= 0 ) ) {
630
- throw new TypeError ( '"maxBuffer" must be a positive number' ) ;
663
+ throw new errors . RangeError ( 'ERR_VALUE_OUT_OF_RANGE' ,
664
+ 'options.maxBuffer' ,
665
+ 'a positive number' ,
666
+ maxBuffer ) ;
631
667
}
632
668
}
633
669
@@ -636,6 +672,9 @@ function sanitizeKillSignal(killSignal) {
636
672
if ( typeof killSignal === 'string' || typeof killSignal === 'number' ) {
637
673
return convertToValidSignal ( killSignal ) ;
638
674
} else if ( killSignal != null ) {
639
- throw new TypeError ( '"killSignal" must be a string or number' ) ;
675
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
676
+ 'options.killSignal' ,
677
+ [ 'string' , 'number' ] ,
678
+ killSignal ) ;
640
679
}
641
680
}
0 commit comments