From 1e8d110e640c658e4f6ed7540db62d063269ba6c Mon Sep 17 00:00:00 2001 From: Michaƫl Zasso Date: Tue, 27 Feb 2018 14:55:32 +0100 Subject: lib: port errors to new system This is a first batch of updates that touches non-underscored modules in lib. PR-URL: https://github.com/nodejs/node/pull/19034 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Joyee Cheung --- lib/child_process.js | 103 ++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 63 deletions(-) (limited to 'lib/child_process.js') diff --git a/lib/child_process.js b/lib/child_process.js index b190d9c70a..c18329554a 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -31,7 +31,14 @@ const { createPromise, const debug = util.debuglog('child_process'); const { Buffer } = require('buffer'); const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap'); -const errors = require('internal/errors'); +const { + ERR_INVALID_ARG_VALUE, + ERR_CHILD_PROCESS_IPC_REQUIRED, + ERR_CHILD_PROCESS_STDIO_MAXBUFFER, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_OPT_VALUE, + ERR_OUT_OF_RANGE +} = require('internal/errors').codes; const child_process = require('internal/child_process'); const { _validateStdio, @@ -48,7 +55,7 @@ function stdioStringToArray(option) { case 'inherit': return [option, option, option, 'ipc']; default: - throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'stdio', option); + throw new ERR_INVALID_OPT_VALUE('stdio', option); } } @@ -65,9 +72,7 @@ exports.fork = function(modulePath /*, args, options*/) { if (pos < arguments.length && arguments[pos] != null) { if (typeof arguments[pos] !== 'object') { - throw new errors.TypeError('ERR_INVALID_ARG_VALUE', - `arguments[${pos}]`, - arguments[pos]); + throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]); } options = util._extend({}, arguments[pos++]); @@ -95,8 +100,7 @@ exports.fork = function(modulePath /*, args, options*/) { options.stdio = options.silent ? stdioStringToArray('pipe') : stdioStringToArray('inherit'); } else if (options.stdio.indexOf('ipc') === -1) { - throw new errors.Error('ERR_CHILD_PROCESS_IPC_REQUIRED', - 'options.stdio'); + throw new ERR_CHILD_PROCESS_IPC_REQUIRED('options.stdio'); } options.execPath = options.execPath || process.execPath; @@ -200,7 +204,7 @@ exports.execFile = function(file /*, args, options, callback*/) { } if (!callback && pos < arguments.length && arguments[pos] != null) { - throw new errors.TypeError('ERR_INVALID_ARG_VALUE', 'args', arguments[pos]); + throw new ERR_INVALID_ARG_VALUE('args', arguments[pos]); } // Validate the timeout, if present. @@ -327,8 +331,7 @@ exports.execFile = function(file /*, args, options, callback*/) { stdoutLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length; if (stdoutLen > options.maxBuffer) { - ex = new errors.RangeError('ERR_CHILD_PROCESS_STDIO_MAXBUFFER', - 'stdout'); + ex = new ERR_CHILD_PROCESS_STDIO_MAXBUFFER('stdout'); kill(); } else if (encoding) { _stdout += chunk; @@ -346,8 +349,7 @@ exports.execFile = function(file /*, args, options, callback*/) { stderrLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length; if (stderrLen > options.maxBuffer) { - ex = new errors.RangeError('ERR_CHILD_PROCESS_STDIO_MAXBUFFER', - 'stderr'); + ex = new ERR_CHILD_PROCESS_STDIO_MAXBUFFER('stderr'); kill(); } else if (encoding) { _stderr += chunk; @@ -384,13 +386,13 @@ function _convertCustomFds(options) { function normalizeSpawnArguments(file, args, options) { if (typeof file !== 'string' || file.length === 0) - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'file', 'string', file); + throw new ERR_INVALID_ARG_TYPE('file', 'string', file); if (Array.isArray(args)) { args = args.slice(0); } else if (args !== undefined && (args === null || typeof args !== 'object')) { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'args', 'object', args); + throw new ERR_INVALID_ARG_TYPE('args', 'object', args); } else { options = args; args = []; @@ -399,77 +401,58 @@ function normalizeSpawnArguments(file, args, options) { if (options === undefined) options = {}; else if (options === null || typeof options !== 'object') - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options', - 'object', - options); + throw new ERR_INVALID_ARG_TYPE('options', 'object', options); // Validate the cwd, if present. if (options.cwd != null && typeof options.cwd !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.cwd', - 'string', - options.cwd); + throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd); } // Validate detached, if present. if (options.detached != null && typeof options.detached !== 'boolean') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.detached', - 'boolean', - options.detached); + throw new ERR_INVALID_ARG_TYPE('options.detached', + 'boolean', options.detached); } // Validate the uid, if present. if (options.uid != null && !Number.isInteger(options.uid)) { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.uid', - 'integer', - options.uid); + throw new ERR_INVALID_ARG_TYPE('options.uid', 'integer', options.uid); } // Validate the gid, if present. if (options.gid != null && !Number.isInteger(options.gid)) { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.gid', - 'integer', - options.gid); + throw new ERR_INVALID_ARG_TYPE('options.gid', 'integer', options.gid); } // Validate the shell, if present. if (options.shell != null && typeof options.shell !== 'boolean' && typeof options.shell !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.shell', - ['boolean', 'string'], - options.shell); + throw new ERR_INVALID_ARG_TYPE('options.shell', + ['boolean', 'string'], options.shell); } // Validate argv0, if present. if (options.argv0 != null && typeof options.argv0 !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.argv0', - 'string', - options.argv0); + throw new ERR_INVALID_ARG_TYPE('options.argv0', 'string', options.argv0); } // Validate windowsHide, if present. if (options.windowsHide != null && typeof options.windowsHide !== 'boolean') { - throw new TypeError('"windowsHide" must be a boolean'); + throw new ERR_INVALID_ARG_TYPE('options.windowsHide', + 'boolean', options.windowsHide); } // Validate windowsVerbatimArguments, if present. if (options.windowsVerbatimArguments != null && typeof options.windowsVerbatimArguments !== 'boolean') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.windowsVerbatimArguments', - 'boolean', - options.windowsVerbatimArguments); + throw new ERR_INVALID_ARG_TYPE('options.windowsVerbatimArguments', + 'boolean', + options.windowsVerbatimArguments); } // Make a shallow copy so we don't clobber the user's options object. @@ -584,10 +567,9 @@ function spawnSync(/*file, args, options*/) { } else if (typeof input === 'string') { pipe.input = Buffer.from(input, options.encoding); } else { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - ('options.stdio[' + i + ']'), - ['Buffer', 'Uint8Array', 'string'], - input); + throw new ERR_INVALID_ARG_TYPE(`options.stdio[${i}]`, + ['Buffer', 'Uint8Array', 'string'], + input); } } } @@ -655,20 +637,16 @@ exports.execSync = execSync; function validateTimeout(timeout) { if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) { - throw new errors.RangeError('ERR_OUT_OF_RANGE', - 'timeout', - 'an unsigned integer', - timeout); + throw new ERR_OUT_OF_RANGE('timeout', 'an unsigned integer', timeout); } } function validateMaxBuffer(maxBuffer) { if (maxBuffer != null && !(typeof maxBuffer === 'number' && maxBuffer >= 0)) { - throw new errors.RangeError('ERR_OUT_OF_RANGE', - 'options.maxBuffer', - 'a positive number', - maxBuffer); + throw new ERR_OUT_OF_RANGE('options.maxBuffer', + 'a positive number', + maxBuffer); } } @@ -677,9 +655,8 @@ function sanitizeKillSignal(killSignal) { if (typeof killSignal === 'string' || typeof killSignal === 'number') { return convertToValidSignal(killSignal); } else if (killSignal != null) { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', - 'options.killSignal', - ['string', 'number'], - killSignal); + throw new ERR_INVALID_ARG_TYPE('options.killSignal', + ['string', 'number'], + killSignal); } } -- cgit v1.2.3