Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/child_process: convert to using internal/errors #15090

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Pipe = process.binding('pipe_wrap').Pipe;
const { isUint8Array } = process.binding('util');
const { errname } = process.binding('uv');
const child_process = require('internal/child_process');
const errors = require('internal/errors');

const _validateStdio = child_process._validateStdio;
const setupChannel = child_process.setupChannel;
Expand All @@ -44,7 +45,7 @@ function stdioStringToArray(option) {
case 'inherit':
return [option, option, option, 'ipc'];
default:
throw new TypeError('Incorrect value of stdio option: ' + option);
throw new errors.TypeError('ERR_INVALID_STDIO_OPT', option);
}
}

Expand All @@ -61,7 +62,7 @@ exports.fork = function(modulePath /*, args, options*/) {

if (pos < arguments.length && arguments[pos] != null) {
if (typeof arguments[pos] !== 'object') {
throw new TypeError('Incorrect value of args option');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', `arguments[${pos}]`, 'object');
}

options = util._extend({}, arguments[pos++]);
Expand Down Expand Up @@ -89,7 +90,7 @@ exports.fork = function(modulePath /*, args, options*/) {
options.stdio = options.silent ? stdioStringToArray('pipe') :
stdioStringToArray('inherit');
} else if (options.stdio.indexOf('ipc') === -1) {
throw new TypeError('Forked processes must have an IPC channel');
throw new errors.TypeError('ERR_MISSING_IPC_CHANNEL');
}

options.execPath = options.execPath || process.execPath;
Expand Down Expand Up @@ -192,7 +193,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
}

if (!callback && pos < arguments.length && arguments[pos] != null) {
throw new TypeError('Incorrect value of args option');
throw new errors.TypeError('ERR_OPTIONS_NOT_OBJECT');
}

// Validate the timeout, if present.
Expand Down Expand Up @@ -375,13 +376,13 @@ function _convertCustomFds(options) {

function normalizeSpawnArguments(file, args, options) {
if (typeof file !== 'string' || file.length === 0)
throw new TypeError('"file" argument must be a non-empty string');
throw new errors.TypeError('ERR_INVALID_FILE_ARG');

if (Array.isArray(args)) {
args = args.slice(0);
} else if (args !== undefined &&
(args === null || typeof args !== 'object')) {
throw new TypeError('Incorrect value of args option');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
} else {
options = args;
args = [];
Expand All @@ -390,47 +391,47 @@ function normalizeSpawnArguments(file, args, options) {
if (options === undefined)
options = {};
else if (options === null || typeof options !== 'object')
throw new TypeError('"options" argument must be an object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');

// Validate the cwd, if present.
if (options.cwd != null &&
typeof options.cwd !== 'string') {
throw new TypeError('"cwd" must be a string');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.cwd', 'string');
}

// Validate detached, if present.
if (options.detached != null &&
typeof options.detached !== 'boolean') {
throw new TypeError('"detached" must be a boolean');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.detached', 'boolean');
}

// Validate the uid, if present.
if (options.uid != null && !Number.isInteger(options.uid)) {
throw new TypeError('"uid" must be an integer');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.uid', 'integer');
}

// Validate the gid, if present.
if (options.gid != null && !Number.isInteger(options.gid)) {
throw new TypeError('"gid" must be an integer');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.gid', 'integer');
}

// Validate the shell, if present.
if (options.shell != null &&
typeof options.shell !== 'boolean' &&
typeof options.shell !== 'string') {
throw new TypeError('"shell" must be a boolean or string');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.shell', 'boolean or string');
}

// Validate argv0, if present.
if (options.argv0 != null &&
typeof options.argv0 !== 'string') {
throw new TypeError('"argv0" must be a string');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.argv0', 'string');
}

// Validate windowsVerbatimArguments, if present.
if (options.windowsVerbatimArguments != null &&
typeof options.windowsVerbatimArguments !== 'boolean') {
throw new TypeError('"windowsVerbatimArguments" must be a boolean');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.windowsVerbatimArguments', 'boolean');
}

// Make a shallow copy so we don't clobber the user's options object.
Expand Down Expand Up @@ -540,10 +541,7 @@ function spawnSync(/*file, args, options*/) {
} else if (typeof input === 'string') {
pipe.input = Buffer.from(input, options.encoding);
} else {
throw new TypeError(util.format(
'stdio[%d] should be Buffer, Uint8Array or string not %s',
i,
typeof input));
throw new errors.TypeError('ERR_INVALID_STDIO_TYPE', i, typeof input);
}
}
}
Expand Down Expand Up @@ -612,14 +610,14 @@ exports.execSync = execSync;

function validateTimeout(timeout) {
if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) {
throw new TypeError('"timeout" must be an unsigned integer');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'timeout', 'unsigned integer');
}
}


function validateMaxBuffer(maxBuffer) {
if (maxBuffer != null && !(typeof maxBuffer === 'number' && maxBuffer >= 0)) {
throw new TypeError('"maxBuffer" must be a positive number');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'maxBuffer', 'positive number');
}
}

Expand All @@ -628,6 +626,6 @@ function sanitizeKillSignal(killSignal) {
if (typeof killSignal === 'string' || typeof killSignal === 'number') {
return convertToValidSignal(killSignal);
} else if (killSignal != null) {
throw new TypeError('"killSignal" must be a string or number');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'killSignal', 'string or number');
}
}
4 changes: 4 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ E('ERR_INVALID_CURSOR_POS',
'Cannot set cursor row without setting its column');
E('ERR_INVALID_DOMAIN_NAME', 'Unable to determine the domain name');
E('ERR_INVALID_FD', '"fd" must be a positive integer: %s');
E('ERR_INVALID_FILE_ARG', '"file" argument must be a non-empty string');
E('ERR_INVALID_FILE_URL_HOST',
'File URL host must be "localhost" or empty on %s');
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
Expand All @@ -213,6 +214,8 @@ E('ERR_INVALID_PROTOCOL', (protocol, expectedProtocol) =>
`Protocol "${protocol}" not supported. Expected "${expectedProtocol}"`);
E('ERR_INVALID_REPL_EVAL_CONFIG',
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
E('ERR_INVALID_STDIO_OPT', 'Incorrect value of stdio option: %s');
E('ERR_INVALID_STDIO_TYPE', 'stdio[%d] should be Buffer, Uint8Array or string not %s');
E('ERR_INVALID_SYNC_FORK_INPUT',
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s');
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
Expand All @@ -226,6 +229,7 @@ E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe');
E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks');
E('ERR_METHOD_NOT_IMPLEMENTED', 'The %s method is not implemented');
E('ERR_MISSING_ARGS', missingArgs);
E('ERR_MISSING_IPC_CHANNEL', 'Forked processes must have an IPC channel');
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function');
E('ERR_NAPI_CONS_PROTOTYPE_OBJECT', 'Constructor.prototype must be an object');
Expand Down