Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
timers: Migrate to use internal/errors
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#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]>
  • Loading branch information
starkwang authored and Olivia Hugger committed Aug 30, 2017
1 parent 7a56a5d commit 1946279
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 8 additions & 6 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-timers-enroll-invalid-msecs.js
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 2 deletions test/parallel/test-timers-throw-when-cb-not-function.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

function doSetTimeout(callback, after) {
Expand All @@ -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);
Expand Down

0 comments on commit 1946279

Please sign in to comment.