Skip to content

Commit 4d893e0

Browse files
starkwangBridgeAR
authored andcommitted
timers: Migrate to use internal/errors
PR-URL: #14659 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 237a42d commit 4d893e0

6 files changed

+61
-8
lines changed

doc/api/errors.md

+5
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,11 @@ are most likely an indication of a bug within Node.js itself.
11411141
Used when the V8 BreakIterator API is used but the full ICU data set is not
11421142
installed.
11431143

1144+
<a id="ERR_VALUE_OUT_OF_RANGE"></a>
1145+
### ERR_VALUE_OUT_OF_RANGE
1146+
1147+
Used when a given value is out of the accepted range.
1148+
11441149
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE
11451150
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
11461151
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' +
268268
'See https://github.com/nodejs/node/wiki/Intl');
269269
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
270270
'At least one valid performance entry type is required');
271+
E('ERR_VALUE_OUT_OF_RANGE', 'The value of "%s" must be %s. Received "%s"');
271272

272273
function invalidArgType(name, expected, actual) {
273274
assert(name, 'name is required');

lib/timers.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const { createPromise, promiseResolve } = process.binding('util');
2929
const async_hooks = require('async_hooks');
3030
const assert = require('assert');
3131
const util = require('util');
32+
const errors = require('internal/errors');
3233
const debug = util.debuglog('timer');
3334
const kOnTimeout = TimerWrap.kOnTimeout | 0;
3435
const initTriggerId = async_hooks.initTriggerId;
@@ -389,12 +390,13 @@ const unenroll = exports.unenroll = function(item) {
389390
// Using existing objects as timers slightly reduces object overhead.
390391
exports.enroll = function(item, msecs) {
391392
if (typeof msecs !== 'number') {
392-
throw new TypeError('"msecs" argument must be a number');
393+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
394+
'number', msecs);
393395
}
394396

395397
if (msecs < 0 || !isFinite(msecs)) {
396-
throw new RangeError('"msecs" argument must be ' +
397-
'a non-negative finite number');
398+
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
399+
'a non-negative finite number', msecs);
398400
}
399401

400402
// if this item was already in a list somewhere
@@ -418,7 +420,7 @@ exports.enroll = function(item, msecs) {
418420

419421
function setTimeout(callback, after, arg1, arg2, arg3) {
420422
if (typeof callback !== 'function') {
421-
throw new TypeError('"callback" argument must be a function');
423+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
422424
}
423425

424426
var len = arguments.length;
@@ -515,7 +517,7 @@ const clearTimeout = exports.clearTimeout = function(timer) {
515517

516518
exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
517519
if (typeof callback !== 'function') {
518-
throw new TypeError('"callback" argument must be a function');
520+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
519521
}
520522

521523
var len = arguments.length;
@@ -810,7 +812,7 @@ function Immediate() {
810812

811813
function setImmediate(callback, arg1, arg2, arg3) {
812814
if (typeof callback !== 'function') {
813-
throw new TypeError('"callback" argument must be a function');
815+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
814816
}
815817

816818
var i, args;

test/parallel/test-internal-errors.js

+5
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ assert.strictEqual(
256256
'Method must be a valid HTTP token ["foo"]'
257257
);
258258

259+
assert.strictEqual(
260+
errors.message('ERR_VALUE_OUT_OF_RANGE', ['A', 'some values', 'B']),
261+
'The value of "A" must be some values. Received "B"'
262+
);
263+
259264
assert.strictEqual(
260265
errors.message('ERR_UNESCAPED_CHARACTERS', ['Request path']),
261266
'Request path contains unescaped characters'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const timers = require('timers');
5+
const assert = require('assert');
6+
7+
[
8+
{},
9+
[],
10+
'foo',
11+
() => { },
12+
Symbol('foo')
13+
].forEach((val) => {
14+
assert.throws(
15+
() => timers.enroll({}, val),
16+
common.expectsError({
17+
code: 'ERR_INVALID_ARG_TYPE',
18+
type: TypeError
19+
})
20+
);
21+
});
22+
23+
[
24+
-1,
25+
Infinity,
26+
NaN
27+
].forEach((val) => {
28+
assert.throws(
29+
() => timers.enroll({}, val),
30+
common.expectsError({
31+
code: 'ERR_VALUE_OUT_OF_RANGE',
32+
type: RangeError,
33+
message: 'The value of "msecs" must be a non-negative ' +
34+
`finite number. Received "${val}"`
35+
})
36+
);
37+
});

test/parallel/test-timers-throw-when-cb-not-function.js

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

55
function doSetTimeout(callback, after) {
@@ -8,7 +8,10 @@ function doSetTimeout(callback, after) {
88
};
99
}
1010

11-
const errMessage = /"callback" argument must be a function/;
11+
const errMessage = common.expectsError({
12+
code: 'ERR_INVALID_CALLBACK',
13+
type: TypeError
14+
}, 18);
1215

1316
assert.throws(doSetTimeout('foo'), errMessage);
1417
assert.throws(doSetTimeout({ foo: 'bar' }), errMessage);

0 commit comments

Comments
 (0)