Permalink
Browse files

timers: Migrate to use internal/errors

PR-URL: #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>
  • Loading branch information...
starkwang authored and BridgeAR committed Aug 7, 2017
1 parent 237a42d commit 4d893e093a539518a201e4a4f40adc26b8bbf74e
View
@@ -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
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
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
View
@@ -268,6 +268,7 @@ E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' +
'See https://github.com/nodejs/node/wiki/Intl');
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
'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) {
assert(name, 'name is required');
View
@@ -29,6 +29,7 @@ const { createPromise, promiseResolve } = process.binding('util');
const async_hooks = require('async_hooks');
const assert = require('assert');
const util = require('util');
const errors = require('internal/errors');
const debug = util.debuglog('timer');
const kOnTimeout = TimerWrap.kOnTimeout | 0;
const initTriggerId = async_hooks.initTriggerId;
@@ -389,12 +390,13 @@ const unenroll = exports.unenroll = function(item) {
// Using existing objects as timers slightly reduces object overhead.
exports.enroll = function(item, msecs) {
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)) {
throw new RangeError('"msecs" argument must be ' +
'a non-negative finite number');
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
'a non-negative finite number', msecs);
}
// 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) {
if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function');
throw new errors.TypeError('ERR_INVALID_CALLBACK');
}
var len = arguments.length;
@@ -515,7 +517,7 @@ const clearTimeout = exports.clearTimeout = function(timer) {
exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function');
throw new errors.TypeError('ERR_INVALID_CALLBACK');
}
var len = arguments.length;
@@ -810,7 +812,7 @@ function Immediate() {
function setImmediate(callback, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function');
throw new errors.TypeError('ERR_INVALID_CALLBACK');
}
var i, args;
@@ -256,6 +256,11 @@ assert.strictEqual(
'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(
errors.message('ERR_UNESCAPED_CHARACTERS', ['Request path']),
'Request path contains unescaped characters'
@@ -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}"`
})
);
});
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
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: 'bar' }), errMessage);

0 comments on commit 4d893e0

Please sign in to comment.