Skip to content

Commit 095357e

Browse files
BridgeARrefack
authored andcommitted
lib: tweak use of internal/errors
In addition refactor common.throws to common.expectsError PR-URL: #13829 Refs: #11273 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 8979b4f commit 095357e

6 files changed

+49
-53
lines changed

lib/internal/child_process.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ ChildProcess.prototype.spawn = function(options) {
284284
options.envPairs = [];
285285
else if (!Array.isArray(options.envPairs)) {
286286
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.envPairs',
287-
'array', options.envPairs);
287+
'Array', options.envPairs);
288288
}
289289

290290
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
@@ -301,7 +301,7 @@ ChildProcess.prototype.spawn = function(options) {
301301
else if (options.args === undefined)
302302
this.spawnargs = [];
303303
else
304-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'array',
304+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options.args', 'Array',
305305
options.args);
306306

307307
var err = this._handle.spawn(options);

lib/internal/util.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,17 @@ function getConstructorOf(obj) {
201201
const kCustomPromisifiedSymbol = Symbol('util.promisify.custom');
202202
const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
203203

204-
function promisify(orig) {
205-
if (typeof orig !== 'function')
204+
function promisify(original) {
205+
if (typeof original !== 'function')
206206
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
207207

208-
if (orig[kCustomPromisifiedSymbol]) {
209-
const fn = orig[kCustomPromisifiedSymbol];
208+
if (original[kCustomPromisifiedSymbol]) {
209+
const fn = original[kCustomPromisifiedSymbol];
210210
if (typeof fn !== 'function') {
211-
throw new TypeError('The [util.promisify.custom] property must be ' +
212-
'a function');
211+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
212+
'util.promisify.custom',
213+
'function',
214+
fn);
213215
}
214216
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
215217
value: fn, enumerable: false, writable: false, configurable: true
@@ -219,12 +221,12 @@ function promisify(orig) {
219221

220222
// Names to create an object from in case the callback receives multiple
221223
// arguments, e.g. ['stdout', 'stderr'] for child_process.exec.
222-
const argumentNames = orig[kCustomPromisifyArgsSymbol];
224+
const argumentNames = original[kCustomPromisifyArgsSymbol];
223225

224226
function fn(...args) {
225227
const promise = createPromise();
226228
try {
227-
orig.call(this, ...args, (err, ...values) => {
229+
original.call(this, ...args, (err, ...values) => {
228230
if (err) {
229231
promiseReject(promise, err);
230232
} else if (argumentNames !== undefined && values.length > 1) {
@@ -242,12 +244,15 @@ function promisify(orig) {
242244
return promise;
243245
}
244246

245-
Object.setPrototypeOf(fn, Object.getPrototypeOf(orig));
247+
Object.setPrototypeOf(fn, Object.getPrototypeOf(original));
246248

247249
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
248250
value: fn, enumerable: false, writable: false, configurable: true
249251
});
250-
return Object.defineProperties(fn, Object.getOwnPropertyDescriptors(orig));
252+
return Object.defineProperties(
253+
fn,
254+
Object.getOwnPropertyDescriptors(original)
255+
);
251256
}
252257

253258
promisify.custom = kCustomPromisifiedSymbol;

test/parallel/test-child-process-constructor.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ function typeName(value) {
1414
const child = new ChildProcess();
1515

1616
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
17-
assert.throws(() => {
17+
common.expectsError(() => {
1818
child.spawn(options);
19-
}, common.expectsError({
19+
}, {
2020
code: 'ERR_INVALID_ARG_TYPE',
2121
type: TypeError,
2222
message: 'The "options" argument must be of type object. ' +
2323
`Received type ${typeName(options)}`
24-
}));
24+
});
2525
});
2626
}
2727

@@ -30,14 +30,14 @@ function typeName(value) {
3030
const child = new ChildProcess();
3131

3232
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
33-
assert.throws(() => {
33+
common.expectsError(() => {
3434
child.spawn({ file });
35-
}, common.expectsError({
35+
}, {
3636
code: 'ERR_INVALID_ARG_TYPE',
3737
type: TypeError,
3838
message: 'The "options.file" property must be of type string. ' +
3939
`Received type ${typeName(file)}`
40-
}));
40+
});
4141
});
4242
}
4343

@@ -46,14 +46,14 @@ function typeName(value) {
4646
const child = new ChildProcess();
4747

4848
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
49-
assert.throws(() => {
49+
common.expectsError(() => {
5050
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
51-
}, common.expectsError({
51+
}, {
5252
code: 'ERR_INVALID_ARG_TYPE',
5353
type: TypeError,
54-
message: 'The "options.envPairs" property must be of type array. ' +
54+
message: 'The "options.envPairs" property must be of type Array. ' +
5555
`Received type ${typeName(envPairs)}`
56-
}));
56+
});
5757
});
5858
}
5959

@@ -62,14 +62,14 @@ function typeName(value) {
6262
const child = new ChildProcess();
6363

6464
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
65-
assert.throws(() => {
65+
common.expectsError(() => {
6666
child.spawn({ file: 'foo', args });
67-
}, common.expectsError({
67+
}, {
6868
code: 'ERR_INVALID_ARG_TYPE',
6969
type: TypeError,
70-
message: 'The "options.args" property must be of type array. ' +
70+
message: 'The "options.args" property must be of type Array. ' +
7171
`Received type ${typeName(args)}`
72-
}));
72+
});
7373
});
7474
}
7575

@@ -86,8 +86,9 @@ assert.strictEqual(child.hasOwnProperty('pid'), true);
8686
assert(Number.isInteger(child.pid));
8787

8888
// try killing with invalid signal
89-
assert.throws(() => {
90-
child.kill('foo');
91-
}, common.expectsError({ code: 'ERR_UNKNOWN_SIGNAL', type: TypeError }));
89+
common.expectsError(
90+
() => { child.kill('foo'); },
91+
{ code: 'ERR_UNKNOWN_SIGNAL', type: TypeError }
92+
);
9293

9394
assert.strictEqual(child.kill(), true);

test/parallel/test-fs-open-flags.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,26 @@ assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
5555
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
5656
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
5757

58-
const expectedError =
59-
common.expectsError({ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }, 23);
60-
6158
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
6259
.split(' ')
6360
.forEach(function(flags) {
64-
assert.throws(
61+
common.expectsError(
6562
() => stringToFlags(flags),
66-
expectedError
63+
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
6764
);
6865
});
6966

70-
assert.throws(
67+
common.expectsError(
7168
() => stringToFlags({}),
72-
expectedError
69+
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
7370
);
7471

75-
assert.throws(
72+
common.expectsError(
7673
() => stringToFlags(true),
77-
expectedError
74+
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
7875
);
7976

80-
assert.throws(
77+
common.expectsError(
8178
() => stringToFlags(null),
82-
expectedError
79+
{ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }
8380
);
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
'use strict';
22

33
const common = require('../common');
4-
const assert = require('assert');
54
const fs = require('fs');
65

76
const encoding = 'foo-8';
87
const filename = 'bar.txt';
9-
const expectedError = common.expectsError({
10-
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
11-
type: TypeError,
12-
});
13-
14-
assert.throws(
8+
common.expectsError(
159
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
16-
expectedError
10+
{ code: 'ERR_INVALID_OPT_VALUE_ENCODING', type: TypeError }
1711
);

test/parallel/test-util-promisify.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ const stat = promisify(fs.stat);
3737
{
3838
function fn() {}
3939
fn[promisify.custom] = 42;
40-
assert.throws(
41-
() => promisify(fn),
42-
(err) => err instanceof TypeError &&
43-
err.message === 'The [util.promisify.custom] property must ' +
44-
'be a function');
40+
common.expectsError(
41+
() => promisify(fn),
42+
{ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }
43+
);
4544
}
4645

4746
{

0 commit comments

Comments
 (0)