lib: tweak use of internal/errors
In addition refactor common.throws to common.expectsError PR-URL: https://github.com/nodejs/node/pull/13829 Refs: https://github.com/nodejs/node/issues/11273 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
8979b4fc9f
commit
095357e26e
@ -284,7 +284,7 @@ ChildProcess.prototype.spawn = function(options) {
|
|||||||
options.envPairs = [];
|
options.envPairs = [];
|
||||||
else if (!Array.isArray(options.envPairs)) {
|
else if (!Array.isArray(options.envPairs)) {
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.envPairs',
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.envPairs',
|
||||||
'array', options.envPairs);
|
'Array', options.envPairs);
|
||||||
}
|
}
|
||||||
|
|
||||||
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
|
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
|
||||||
@ -301,7 +301,7 @@ ChildProcess.prototype.spawn = function(options) {
|
|||||||
else if (options.args === undefined)
|
else if (options.args === undefined)
|
||||||
this.spawnargs = [];
|
this.spawnargs = [];
|
||||||
else
|
else
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'array',
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'Array',
|
||||||
options.args);
|
options.args);
|
||||||
|
|
||||||
var err = this._handle.spawn(options);
|
var err = this._handle.spawn(options);
|
||||||
|
@ -201,15 +201,17 @@ function getConstructorOf(obj) {
|
|||||||
const kCustomPromisifiedSymbol = Symbol('util.promisify.custom');
|
const kCustomPromisifiedSymbol = Symbol('util.promisify.custom');
|
||||||
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
|
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
|
||||||
|
|
||||||
function promisify(orig) {
|
function promisify(original) {
|
||||||
if (typeof orig !== 'function')
|
if (typeof original !== 'function')
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
|
||||||
|
|
||||||
if (orig[kCustomPromisifiedSymbol]) {
|
if (original[kCustomPromisifiedSymbol]) {
|
||||||
const fn = orig[kCustomPromisifiedSymbol];
|
const fn = original[kCustomPromisifiedSymbol];
|
||||||
if (typeof fn !== 'function') {
|
if (typeof fn !== 'function') {
|
||||||
throw new TypeError('The [util.promisify.custom] property must be ' +
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
|
||||||
'a function');
|
'util.promisify.custom',
|
||||||
|
'function',
|
||||||
|
fn);
|
||||||
}
|
}
|
||||||
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
|
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
|
||||||
value: fn, enumerable: false, writable: false, configurable: true
|
value: fn, enumerable: false, writable: false, configurable: true
|
||||||
@ -219,12 +221,12 @@ function promisify(orig) {
|
|||||||
|
|
||||||
// Names to create an object from in case the callback receives multiple
|
// Names to create an object from in case the callback receives multiple
|
||||||
// arguments, e.g. ['stdout', 'stderr'] for child_process.exec.
|
// arguments, e.g. ['stdout', 'stderr'] for child_process.exec.
|
||||||
const argumentNames = orig[kCustomPromisifyArgsSymbol];
|
const argumentNames = original[kCustomPromisifyArgsSymbol];
|
||||||
|
|
||||||
function fn(...args) {
|
function fn(...args) {
|
||||||
const promise = createPromise();
|
const promise = createPromise();
|
||||||
try {
|
try {
|
||||||
orig.call(this, ...args, (err, ...values) => {
|
original.call(this, ...args, (err, ...values) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
promiseReject(promise, err);
|
promiseReject(promise, err);
|
||||||
} else if (argumentNames !== undefined && values.length > 1) {
|
} else if (argumentNames !== undefined && values.length > 1) {
|
||||||
@ -242,12 +244,15 @@ function promisify(orig) {
|
|||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.setPrototypeOf(fn, Object.getPrototypeOf(orig));
|
Object.setPrototypeOf(fn, Object.getPrototypeOf(original));
|
||||||
|
|
||||||
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
|
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
|
||||||
value: fn, enumerable: false, writable: false, configurable: true
|
value: fn, enumerable: false, writable: false, configurable: true
|
||||||
});
|
});
|
||||||
return Object.defineProperties(fn, Object.getOwnPropertyDescriptors(orig));
|
return Object.defineProperties(
|
||||||
|
fn,
|
||||||
|
Object.getOwnPropertyDescriptors(original)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
promisify.custom = kCustomPromisifiedSymbol;
|
promisify.custom = kCustomPromisifiedSymbol;
|
||||||
|
@ -14,14 +14,14 @@ function typeName(value) {
|
|||||||
const child = new ChildProcess();
|
const child = new ChildProcess();
|
||||||
|
|
||||||
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
|
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
|
||||||
assert.throws(() => {
|
common.expectsError(() => {
|
||||||
child.spawn(options);
|
child.spawn(options);
|
||||||
}, common.expectsError({
|
}, {
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "options" argument must be of type object. ' +
|
message: 'The "options" argument must be of type object. ' +
|
||||||
`Received type ${typeName(options)}`
|
`Received type ${typeName(options)}`
|
||||||
}));
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,14 +30,14 @@ function typeName(value) {
|
|||||||
const child = new ChildProcess();
|
const child = new ChildProcess();
|
||||||
|
|
||||||
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
|
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
|
||||||
assert.throws(() => {
|
common.expectsError(() => {
|
||||||
child.spawn({ file });
|
child.spawn({ file });
|
||||||
}, common.expectsError({
|
}, {
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "options.file" property must be of type string. ' +
|
message: 'The "options.file" property must be of type string. ' +
|
||||||
`Received type ${typeName(file)}`
|
`Received type ${typeName(file)}`
|
||||||
}));
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,14 +46,14 @@ function typeName(value) {
|
|||||||
const child = new ChildProcess();
|
const child = new ChildProcess();
|
||||||
|
|
||||||
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
|
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
|
||||||
assert.throws(() => {
|
common.expectsError(() => {
|
||||||
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
|
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
|
||||||
}, common.expectsError({
|
}, {
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "options.envPairs" property must be of type array. ' +
|
message: 'The "options.envPairs" property must be of type Array. ' +
|
||||||
`Received type ${typeName(envPairs)}`
|
`Received type ${typeName(envPairs)}`
|
||||||
}));
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,14 +62,14 @@ function typeName(value) {
|
|||||||
const child = new ChildProcess();
|
const child = new ChildProcess();
|
||||||
|
|
||||||
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
|
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
|
||||||
assert.throws(() => {
|
common.expectsError(() => {
|
||||||
child.spawn({ file: 'foo', args });
|
child.spawn({ file: 'foo', args });
|
||||||
}, common.expectsError({
|
}, {
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The "options.args" property must be of type array. ' +
|
message: 'The "options.args" property must be of type Array. ' +
|
||||||
`Received type ${typeName(args)}`
|
`Received type ${typeName(args)}`
|
||||||
}));
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,8 +86,9 @@ assert.strictEqual(child.hasOwnProperty('pid'), true);
|
|||||||
assert(Number.isInteger(child.pid));
|
assert(Number.isInteger(child.pid));
|
||||||
|
|
||||||
// try killing with invalid signal
|
// try killing with invalid signal
|
||||||
assert.throws(() => {
|
common.expectsError(
|
||||||
child.kill('foo');
|
() => { child.kill('foo'); },
|
||||||
}, common.expectsError({ code: 'ERR_UNKNOWN_SIGNAL', type: TypeError }));
|
{ code: 'ERR_UNKNOWN_SIGNAL', type: TypeError }
|
||||||
|
);
|
||||||
|
|
||||||
assert.strictEqual(child.kill(), true);
|
assert.strictEqual(child.kill(), true);
|
||||||
|
@ -55,29 +55,26 @@ assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
|
|||||||
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
||||||
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
||||||
|
|
||||||
const expectedError =
|
|
||||||
common.expectsError({ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }, 23);
|
|
||||||
|
|
||||||
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
|
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
|
||||||
.split(' ')
|
.split(' ')
|
||||||
.forEach(function(flags) {
|
.forEach(function(flags) {
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => stringToFlags(flags),
|
() => stringToFlags(flags),
|
||||||
expectedError
|
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => stringToFlags({}),
|
() => stringToFlags({}),
|
||||||
expectedError
|
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => stringToFlags(true),
|
() => stringToFlags(true),
|
||||||
expectedError
|
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => stringToFlags(null),
|
() => stringToFlags(null),
|
||||||
expectedError
|
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
|
||||||
);
|
);
|
||||||
|
@ -1,17 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const encoding = 'foo-8';
|
const encoding = 'foo-8';
|
||||||
const filename = 'bar.txt';
|
const filename = 'bar.txt';
|
||||||
const expectedError = common.expectsError({
|
common.expectsError(
|
||||||
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
|
|
||||||
type: TypeError,
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.throws(
|
|
||||||
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
|
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
|
||||||
expectedError
|
{ code: 'ERR_INVALID_OPT_VALUE_ENCODING', type: TypeError }
|
||||||
);
|
);
|
||||||
|
@ -37,11 +37,10 @@ const stat = promisify(fs.stat);
|
|||||||
{
|
{
|
||||||
function fn() {}
|
function fn() {}
|
||||||
fn[promisify.custom] = 42;
|
fn[promisify.custom] = 42;
|
||||||
assert.throws(
|
common.expectsError(
|
||||||
() => promisify(fn),
|
() => promisify(fn),
|
||||||
(err) => err instanceof TypeError &&
|
{ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }
|
||||||
err.message === 'The [util.promisify.custom] property must ' +
|
);
|
||||||
'be a function');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user