Browse Source

timers: Migrate to use internal/errors

PR-URL: https://github.com/nodejs/node/pull/14659
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
canary-base
Weijia Wang 8 years ago
committed by Ruben Bridgewater
parent
commit
4d893e093a
No known key found for this signature in database GPG Key ID: F07496B3EB3C1762
  1. 5
      doc/api/errors.md
  2. 1
      lib/internal/errors.js
  3. 14
      lib/timers.js
  4. 5
      test/parallel/test-internal-errors.js
  5. 37
      test/parallel/test-timers-enroll-invalid-msecs.js
  6. 7
      test/parallel/test-timers-throw-when-cb-not-function.js

5
doc/api/errors.md

@ -1141,6 +1141,11 @@ are most likely an indication of a bug within Node.js itself.
Used when the V8 BreakIterator API is used but the full ICU data set is not Used when the V8 BreakIterator API is used but the full ICU data set is not
installed. installed.
<a id="ERR_VALUE_OUT_OF_RANGE"></a>
### ERR_VALUE_OUT_OF_RANGE
Used when a given value is out of the accepted range.
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE [`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal [`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback [`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback

1
lib/internal/errors.js

@ -268,6 +268,7 @@ E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' +
'See https://github.com/nodejs/node/wiki/Intl'); 'See https://github.com/nodejs/node/wiki/Intl');
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE', E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
'At least one valid performance entry type is required'); 'At least one valid performance entry type is required');
E('ERR_VALUE_OUT_OF_RANGE', 'The value of "%s" must be %s. Received "%s"');
function invalidArgType(name, expected, actual) { function invalidArgType(name, expected, actual) {
assert(name, 'name is required'); assert(name, 'name is required');

14
lib/timers.js

@ -29,6 +29,7 @@ const { createPromise, promiseResolve } = process.binding('util');
const async_hooks = require('async_hooks'); const async_hooks = require('async_hooks');
const assert = require('assert'); const assert = require('assert');
const util = require('util'); const util = require('util');
const errors = require('internal/errors');
const debug = util.debuglog('timer'); const debug = util.debuglog('timer');
const kOnTimeout = TimerWrap.kOnTimeout | 0; const kOnTimeout = TimerWrap.kOnTimeout | 0;
const initTriggerId = async_hooks.initTriggerId; const initTriggerId = async_hooks.initTriggerId;
@ -389,12 +390,13 @@ const unenroll = exports.unenroll = function(item) {
// Using existing objects as timers slightly reduces object overhead. // Using existing objects as timers slightly reduces object overhead.
exports.enroll = function(item, msecs) { exports.enroll = function(item, msecs) {
if (typeof msecs !== 'number') { if (typeof msecs !== 'number') {
throw new TypeError('"msecs" argument must be a number'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
'number', msecs);
} }
if (msecs < 0 || !isFinite(msecs)) { if (msecs < 0 || !isFinite(msecs)) {
throw new RangeError('"msecs" argument must be ' + throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
'a non-negative finite number'); 'a non-negative finite number', msecs);
} }
// if this item was already in a list somewhere // if this item was already in a list somewhere
@ -418,7 +420,7 @@ exports.enroll = function(item, msecs) {
function setTimeout(callback, after, arg1, arg2, arg3) { function setTimeout(callback, after, arg1, arg2, arg3) {
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function'); throw new errors.TypeError('ERR_INVALID_CALLBACK');
} }
var len = arguments.length; var len = arguments.length;
@ -515,7 +517,7 @@ const clearTimeout = exports.clearTimeout = function(timer) {
exports.setInterval = function(callback, repeat, arg1, arg2, arg3) { exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function'); throw new errors.TypeError('ERR_INVALID_CALLBACK');
} }
var len = arguments.length; var len = arguments.length;
@ -810,7 +812,7 @@ function Immediate() {
function setImmediate(callback, arg1, arg2, arg3) { function setImmediate(callback, arg1, arg2, arg3) {
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function'); throw new errors.TypeError('ERR_INVALID_CALLBACK');
} }
var i, args; var i, args;

5
test/parallel/test-internal-errors.js

@ -256,6 +256,11 @@ assert.strictEqual(
'Method must be a valid HTTP token ["foo"]' 'Method must be a valid HTTP token ["foo"]'
); );
assert.strictEqual(
errors.message('ERR_VALUE_OUT_OF_RANGE', ['A', 'some values', 'B']),
'The value of "A" must be some values. Received "B"'
);
assert.strictEqual( assert.strictEqual(
errors.message('ERR_UNESCAPED_CHARACTERS', ['Request path']), errors.message('ERR_UNESCAPED_CHARACTERS', ['Request path']),
'Request path contains unescaped characters' 'Request path contains unescaped characters'

37
test/parallel/test-timers-enroll-invalid-msecs.js

@ -0,0 +1,37 @@
'use strict';
const common = require('../common');
const timers = require('timers');
const assert = require('assert');
[
{},
[],
'foo',
() => { },
Symbol('foo')
].forEach((val) => {
assert.throws(
() => timers.enroll({}, val),
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
})
);
});
[
-1,
Infinity,
NaN
].forEach((val) => {
assert.throws(
() => timers.enroll({}, val),
common.expectsError({
code: 'ERR_VALUE_OUT_OF_RANGE',
type: RangeError,
message: 'The value of "msecs" must be a non-negative ' +
`finite number. Received "${val}"`
})
);
});

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

@ -1,5 +1,5 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
function doSetTimeout(callback, after) { function doSetTimeout(callback, after) {
@ -8,7 +8,10 @@ function doSetTimeout(callback, after) {
}; };
} }
const errMessage = /"callback" argument must be a function/; const errMessage = common.expectsError({
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}, 18);
assert.throws(doSetTimeout('foo'), errMessage); assert.throws(doSetTimeout('foo'), errMessage);
assert.throws(doSetTimeout({ foo: 'bar' }), errMessage); assert.throws(doSetTimeout({ foo: 'bar' }), errMessage);

Loading…
Cancel
Save