From 4d893e093a539518a201e4a4f40adc26b8bbf74e Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Mon, 7 Aug 2017 14:42:55 +0800 Subject: [PATCH] timers: Migrate to use internal/errors PR-URL: https://github.com/nodejs/node/pull/14659 Reviewed-By: Refael Ackermann Reviewed-By: Jeremiah Senkpiel Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Ruben Bridgewater --- doc/api/errors.md | 5 +++ lib/internal/errors.js | 1 + lib/timers.js | 14 ++++--- test/parallel/test-internal-errors.js | 5 +++ .../test-timers-enroll-invalid-msecs.js | 37 +++++++++++++++++++ .../test-timers-throw-when-cb-not-function.js | 7 +++- 6 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 test/parallel/test-timers-enroll-invalid-msecs.js diff --git a/doc/api/errors.md b/doc/api/errors.md index b58a12010540e9..7b29ab6ddbaae9 100644 --- a/doc/api/errors.md +++ b/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 installed. + +### 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 diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 1defdcc2687b3b..c90cd82e305d3c 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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'); diff --git a/lib/timers.js b/lib/timers.js index 38d83f8c17bd8f..50abf5c254db5b 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -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; diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 627dd2f7817d49..c0c4e48761837f 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -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' diff --git a/test/parallel/test-timers-enroll-invalid-msecs.js b/test/parallel/test-timers-enroll-invalid-msecs.js new file mode 100644 index 00000000000000..0ecad0bd2a3191 --- /dev/null +++ b/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}"` + }) + ); +}); diff --git a/test/parallel/test-timers-throw-when-cb-not-function.js b/test/parallel/test-timers-throw-when-cb-not-function.js index 7751f0fb2e03a1..9f7f653d3d3afc 100644 --- a/test/parallel/test-timers-throw-when-cb-not-function.js +++ b/test/parallel/test-timers-throw-when-cb-not-function.js @@ -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);