@@ -29,6 +29,7 @@ const { createPromise,
2929const debug = util . debuglog ( 'child_process' ) ;
3030const { Buffer } = require ( 'buffer' ) ;
3131const { Pipe, constants : PipeConstants } = process . binding ( 'pipe_wrap' ) ;
32+ const errors = require ( 'internal/errors' ) ;
3233const { errname } = process . binding ( 'uv' ) ;
3334const child_process = require ( 'internal/child_process' ) ;
3435const {
@@ -46,7 +47,7 @@ function stdioStringToArray(option) {
4647 case 'inherit' :
4748 return [ option , option , option , 'ipc' ] ;
4849 default :
49- throw new TypeError ( 'Incorrect value of stdio option: ' + option ) ;
50+ throw new errors . TypeError ( 'ERR_INVALID_OPT_VALUE' , ' stdio' , option ) ;
5051 }
5152}
5253
@@ -63,7 +64,9 @@ exports.fork = function(modulePath /*, args, options*/) {
6364
6465 if ( pos < arguments . length && arguments [ pos ] != null ) {
6566 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 ] ) ;
6770 }
6871
6972 options = util . _extend ( { } , arguments [ pos ++ ] ) ;
@@ -91,7 +94,8 @@ exports.fork = function(modulePath /*, args, options*/) {
9194 options . stdio = options . silent ? stdioStringToArray ( 'pipe' ) :
9295 stdioStringToArray ( 'inherit' ) ;
9396 } 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' ) ;
9599 }
96100
97101 options . execPath = options . execPath || process . execPath ;
@@ -195,7 +199,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
195199 }
196200
197201 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 ] ) ;
199203 }
200204
201205 // Validate the timeout, if present.
@@ -322,7 +326,8 @@ exports.execFile = function(file /*, args, options, callback*/) {
322326 stdoutLen += encoding ? Buffer . byteLength ( chunk , encoding ) : chunk . length ;
323327
324328 if ( stdoutLen > options . maxBuffer ) {
325- ex = new Error ( 'stdout maxBuffer exceeded' ) ;
329+ ex = new errors . RangeError ( 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ,
330+ 'stdout' ) ;
326331 kill ( ) ;
327332 } else if ( encoding ) {
328333 _stdout += chunk ;
@@ -340,7 +345,8 @@ exports.execFile = function(file /*, args, options, callback*/) {
340345 stderrLen += encoding ? Buffer . byteLength ( chunk , encoding ) : chunk . length ;
341346
342347 if ( stderrLen > options . maxBuffer ) {
343- ex = new Error ( 'stderr maxBuffer exceeded' ) ;
348+ ex = new errors . RangeError ( 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ,
349+ 'stderr' ) ;
344350 kill ( ) ;
345351 } else if ( encoding ) {
346352 _stderr += chunk ;
@@ -377,13 +383,13 @@ function _convertCustomFds(options) {
377383
378384function normalizeSpawnArguments ( file , args , options ) {
379385 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 ) ;
381387
382388 if ( Array . isArray ( args ) ) {
383389 args = args . slice ( 0 ) ;
384390 } else if ( args !== undefined &&
385391 ( 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 ) ;
387393 } else {
388394 options = args ;
389395 args = [ ] ;
@@ -392,41 +398,62 @@ function normalizeSpawnArguments(file, args, options) {
392398 if ( options === undefined )
393399 options = { } ;
394400 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 ) ;
396405
397406 // Validate the cwd, if present.
398407 if ( options . cwd != null &&
399408 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 ) ;
401413 }
402414
403415 // Validate detached, if present.
404416 if ( options . detached != null &&
405417 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 ) ;
407422 }
408423
409424 // Validate the uid, if present.
410425 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 ) ;
412430 }
413431
414432 // Validate the gid, if present.
415433 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 ) ;
417438 }
418439
419440 // Validate the shell, if present.
420441 if ( options . shell != null &&
421442 typeof options . shell !== 'boolean' &&
422443 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 ) ;
424448 }
425449
426450 // Validate argv0, if present.
427451 if ( options . argv0 != null &&
428452 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 ) ;
430457 }
431458
432459 // Validate windowsHide, if present.
@@ -438,7 +465,10 @@ function normalizeSpawnArguments(file, args, options) {
438465 // Validate windowsVerbatimArguments, if present.
439466 if ( options . windowsVerbatimArguments != null &&
440467 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 ) ;
442472 }
443473
444474 // Make a shallow copy so we don't clobber the user's options object.
@@ -549,10 +579,10 @@ function spawnSync(/*file, args, options*/) {
549579 } else if ( typeof input === 'string' ) {
550580 pipe . input = Buffer . from ( input , options . encoding ) ;
551581 } 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 ) ;
556586 }
557587 }
558588 }
@@ -620,14 +650,20 @@ exports.execSync = execSync;
620650
621651function validateTimeout ( timeout ) {
622652 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 ) ;
624657 }
625658}
626659
627660
628661function validateMaxBuffer ( maxBuffer ) {
629662 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 ) ;
631667 }
632668}
633669
@@ -636,6 +672,9 @@ function sanitizeKillSignal(killSignal) {
636672 if ( typeof killSignal === 'string' || typeof killSignal === 'number' ) {
637673 return convertToValidSignal ( killSignal ) ;
638674 } 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 ) ;
640679 }
641680}
0 commit comments