Skip to content

Commit 062071a

Browse files
sreepurnajastimhdawson
authored andcommitted
errors,process: migrate to use internal/errors.js
PR-URL: #13285 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 7024c5a commit 062071a

6 files changed

+74
-32
lines changed

lib/internal/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
114114
E('ERR_ASSERTION', (msg) => msg);
115115
E('ERR_CONSOLE_WRITABLE_STREAM',
116116
(name) => `Console expects a writable stream instance for ${name}`);
117+
E('ERR_CPU_USAGE', (errMsg) => `Unable to obtain cpu usage ${errMsg}`);
117118
E('ERR_HTTP_HEADERS_SENT',
118119
'Cannot render headers after they are sent to the client');
119120
E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
@@ -160,6 +161,8 @@ E('ERR_SOCKET_BAD_TYPE',
160161
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
161162
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
162163
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
164+
E('ERR_V8BREAKITERATOR', 'full ICU data not installed. ' +
165+
'See https://github.com/nodejs/node/wiki/Intl');
163166
// Add new errors from here...
164167

165168
function invalidArgType(name, expected, actual) {

lib/internal/process.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const errors = require('internal/errors');
34
var _lazyConstants = null;
45

56
function lazyConstants() {
@@ -10,7 +11,7 @@ function lazyConstants() {
1011
}
1112

1213
const assert = process.assert = function(x, msg) {
13-
if (!x) throw new Error(msg || 'assertion error');
14+
if (!x) throw new errors.Error('ERR_ASSERTION', msg || 'assertion error');
1415
};
1516

1617

@@ -28,18 +29,20 @@ function setup_cpuUsage() {
2829
// If a previous value was passed in, ensure it has the correct shape.
2930
if (prevValue) {
3031
if (!previousValueIsValid(prevValue.user)) {
31-
throw new TypeError('value of user property of argument is invalid');
32+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
33+
'preValue.user', 'Number');
3234
}
3335

3436
if (!previousValueIsValid(prevValue.system)) {
35-
throw new TypeError('value of system property of argument is invalid');
37+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
38+
'preValue.system', 'Number');
3639
}
3740
}
3841

3942
// Call the native function to get the current values.
4043
const errmsg = _cpuUsage(cpuValues);
4144
if (errmsg) {
42-
throw new Error('unable to obtain CPU usage: ' + errmsg);
45+
throw new errors.Error('ERR_CPU_USAGE', errmsg);
4346
}
4447

4548
// If a previous value was passed in, return diff of current from previous.
@@ -81,8 +84,8 @@ function setup_hrtime() {
8184
const needsBorrow = nsec < 0;
8285
return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec];
8386
}
84-
85-
throw new TypeError('process.hrtime() only accepts an Array tuple');
87+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
88+
'process.hrtime()', 'Array');
8689
}
8790

8891
return [
@@ -132,8 +135,7 @@ function setupConfig(_source) {
132135
des.value = require('internal/util').deprecate(function v8BreakIterator() {
133136
if (processConfig.hasSmallICU && !processConfig.icuDataDir) {
134137
// Intl.v8BreakIterator() would crash w/ fatal error, so throw instead.
135-
throw new Error('v8BreakIterator: full ICU data not installed. ' +
136-
'See https://github.com/nodejs/node/wiki/Intl');
138+
throw new errors.Error('ERR_V8BREAKITERATOR');
137139
}
138140
return Reflect.construct(oldV8BreakIterator, arguments);
139141
}, 'Intl.v8BreakIterator is deprecated and will be removed soon.',
@@ -161,7 +163,7 @@ function setupKillAndExit() {
161163

162164
// eslint-disable-next-line eqeqeq
163165
if (pid != (pid | 0)) {
164-
throw new TypeError('invalid pid');
166+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pid', 'Number');
165167
}
166168

167169
// preserve null signal
@@ -172,7 +174,7 @@ function setupKillAndExit() {
172174
if (lazyConstants()[sig]) {
173175
err = process._kill(pid, lazyConstants()[sig]);
174176
} else {
175-
throw new Error(`Unknown signal: ${sig}`);
177+
throw new errors.Error('ERR_UNKNOWN_SIGNAL', `${sig}`);
176178
}
177179
}
178180

test/parallel/test-process-assert.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44

55
assert.strictEqual(process.assert(1, 'error'), undefined);
66
assert.throws(() => {
77
process.assert(undefined, 'errorMessage');
8-
}, /^Error: errorMessage$/);
8+
}, common.expectsError({
9+
code: 'ERR_ASSERTION',
10+
type: Error,
11+
message: 'errorMessage'
12+
})
13+
);
914
assert.throws(() => {
1015
process.assert(false);
11-
}, /^Error: assertion error$/);
16+
}, common.expectsError({
17+
code: 'ERR_ASSERTION',
18+
type: Error,
19+
message: 'assertion error'
20+
})
21+
);

test/parallel/test-process-cpuUsage.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
2-
require('../common');
32
const assert = require('assert');
4-
3+
const common = require('../common');
54
const result = process.cpuUsage();
65

76
// Validate the result of calling with no previous value argument.
@@ -32,11 +31,18 @@ for (let i = 0; i < 10; i++) {
3231
assert(diffUsage.user >= 0);
3332
assert(diffUsage.system >= 0);
3433
}
34+
const invalidUserArgument = common.expectsError({
35+
code: 'ERR_INVALID_ARG_TYPE',
36+
type: TypeError,
37+
message: 'The "preValue.user" argument must be of type Number'
38+
});
39+
40+
const invalidSystemArgument = common.expectsError({
41+
code: 'ERR_INVALID_ARG_TYPE',
42+
type: TypeError,
43+
message: 'The "preValue.system" argument must be of type Number'
44+
});
3545

36-
const invalidUserArgument =
37-
/^TypeError: value of user property of argument is invalid$/;
38-
const invalidSystemArgument =
39-
/^TypeError: value of system property of argument is invalid$/;
4046

4147
// Ensure that an invalid shape for the previous value argument throws an error.
4248
assert.throws(() => {

test/parallel/test-process-hrtime.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525

2626
// the default behavior, return an Array "tuple" of numbers
@@ -32,19 +32,25 @@ validateTuple(tuple);
3232
// validate that passing an existing tuple returns another valid tuple
3333
validateTuple(process.hrtime(tuple));
3434

35+
const invalidHrtimeArgument = common.expectsError({
36+
code: 'ERR_INVALID_ARG_TYPE',
37+
type: TypeError,
38+
message: 'The "process.hrtime()" argument must be of type Array'
39+
});
40+
3541
// test that only an Array may be passed to process.hrtime()
3642
assert.throws(() => {
3743
process.hrtime(1);
38-
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
44+
}, invalidHrtimeArgument);
3945
assert.throws(() => {
4046
process.hrtime([]);
41-
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
47+
}, invalidHrtimeArgument);
4248
assert.throws(() => {
4349
process.hrtime([1]);
44-
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
50+
}, invalidHrtimeArgument);
4551
assert.throws(() => {
4652
process.hrtime([1, 2, 3]);
47-
}, /^TypeError: process\.hrtime\(\) only accepts an Array tuple$/);
53+
}, invalidHrtimeArgument);
4854

4955
function validateTuple(tuple) {
5056
assert(Array.isArray(tuple));

test/parallel/test-process-kill-pid.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525

2626
// test variants of pid
@@ -38,20 +38,35 @@ const assert = require('assert');
3838
//
3939
// process.pid, String(process.pid): ourself
4040

41+
const invalidPidArgument = common.expectsError({
42+
code: 'ERR_INVALID_ARG_TYPE',
43+
type: TypeError,
44+
message: 'The "pid" argument must be of type Number'
45+
});
46+
4147
assert.throws(function() { process.kill('SIGTERM'); },
42-
/^TypeError: invalid pid$/);
43-
assert.throws(function() { process.kill(null); }, /^TypeError: invalid pid$/);
48+
invalidPidArgument);
49+
assert.throws(function() { process.kill(null); },
50+
invalidPidArgument);
4451
assert.throws(function() { process.kill(undefined); },
45-
/^TypeError: invalid pid$/);
52+
invalidPidArgument);
4653
assert.throws(function() { process.kill(+'not a number'); },
47-
/^TypeError: invalid pid$/);
48-
assert.throws(function() { process.kill(1 / 0); }, /^TypeError: invalid pid$/);
49-
assert.throws(function() { process.kill(-1 / 0); }, /^TypeError: invalid pid$/);
54+
invalidPidArgument);
55+
assert.throws(function() { process.kill(1 / 0); },
56+
invalidPidArgument);
57+
assert.throws(function() { process.kill(-1 / 0); },
58+
invalidPidArgument);
5059

5160
// Test that kill throws an error for invalid signal
61+
const unknownSignal = common.expectsError({
62+
code: 'ERR_UNKNOWN_SIGNAL',
63+
type: Error,
64+
message: 'Unknown signal: test'
65+
});
66+
5267

5368
assert.throws(function() { process.kill(1, 'test'); },
54-
/^Error: Unknown signal: test$/);
69+
unknownSignal);
5570

5671
// Test kill argument processing in valid cases.
5772
//

0 commit comments

Comments
 (0)