From d022cb1bdd20f10483cbe988a950548df2dfd48c Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Wed, 13 Dec 2017 20:48:43 +0800 Subject: [PATCH] lib: combine similar error codes There two similar error codes in lib: "ERR_VALUE_OUT_OF_RANGE" and "ERR_OUT_OF_RANGE". This change is to reduce them into "ERR_VALUE_OUT_OF_RANGE" Fixes: https://github.com/nodejs/node/issues/17603 PR-URL: https://github.com/nodejs/node/pull/17648 Fixes: https://github.com/nodejs/node/issues/17603 Reviewed-By: Timothy Gu Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Jon Moss Reviewed-By: Anatoli Papirovski Reviewed-By: Anna Henningsen --- doc/api/errors.md | 4 ++-- lib/child_process.js | 4 ++-- lib/events.js | 8 ++++++-- lib/fs.js | 4 ++-- lib/internal/crypto/pbkdf2.js | 5 ++++- lib/internal/errors.js | 12 ++++++++---- lib/internal/http2/core.js | 3 ++- lib/internal/timers.js | 2 +- ...-child-process-spawnsync-validation-errors.js | 2 +- test/parallel/test-crypto-pbkdf2.js | 5 +++-- test/parallel/test-crypto-random.js | 16 ++++++++-------- .../parallel/test-event-emitter-max-listeners.js | 10 ++++++---- test/parallel/test-file-write-stream3.js | 5 +++-- test/parallel/test-fs-read-stream-inherit.js | 5 +++-- test/parallel/test-fs-read-stream.js | 6 +++--- test/parallel/test-internal-errors.js | 14 ++++++++++++-- .../parallel/test-timers-enroll-invalid-msecs.js | 7 ++++--- 17 files changed, 70 insertions(+), 42 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index ed1d6b16661b86..36df9b24a6d993 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1340,7 +1340,7 @@ An operation caused an out-of-memory condition. ### ERR_OUT_OF_RANGE -An input argument value was outside an acceptable range. +A given value is out of the accepted range. ### ERR_PARSE_HISTORY_DATA @@ -1609,7 +1609,7 @@ entry types were found. ### ERR_VALUE_OUT_OF_RANGE -A given value is out of the accepted range. +Superseded by `ERR_OUT_OF_RANGE` ### ERR_ZLIB_BINDING_CLOSED diff --git a/lib/child_process.js b/lib/child_process.js index c4512ab2f47c08..d1c4d2f2903516 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -650,7 +650,7 @@ exports.execSync = execSync; function validateTimeout(timeout) { if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) { - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'timeout', 'an unsigned integer', timeout); @@ -660,7 +660,7 @@ function validateTimeout(timeout) { function validateMaxBuffer(maxBuffer) { if (maxBuffer != null && !(typeof maxBuffer === 'number' && maxBuffer >= 0)) { - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'options.maxBuffer', 'a positive number', maxBuffer); diff --git a/lib/events.js b/lib/events.js index 28a8c4228b3169..9ad5ec376ff8c8 100644 --- a/lib/events.js +++ b/lib/events.js @@ -56,7 +56,10 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', { // greater and not a NaN). if (typeof arg !== 'number' || arg < 0 || arg !== arg) { const errors = lazyErrors(); - throw new errors.TypeError('ERR_OUT_OF_RANGE', 'defaultMaxListeners'); + throw new errors.RangeError('ERR_OUT_OF_RANGE', + 'defaultMaxListeners', + 'a non-negative number', + arg); } defaultMaxListeners = arg; } @@ -78,7 +81,8 @@ EventEmitter.init = function() { EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { if (typeof n !== 'number' || n < 0 || isNaN(n)) { const errors = lazyErrors(); - throw new errors.TypeError('ERR_OUT_OF_RANGE', 'n'); + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'n', + 'a non-negative number', n); } this._maxListeners = n; return this; diff --git a/lib/fs.js b/lib/fs.js index 94da3914c3fa3d..7741e5838bd216 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2353,7 +2353,7 @@ function ReadStream(path, options) { if (this.start > this.end) { const errVal = `{start: ${this.start}, end: ${this.end}}`; - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'start', '<= "end"', errVal); @@ -2511,7 +2511,7 @@ function WriteStream(path, options) { } if (this.start < 0) { const errVal = `{start: ${this.start}}`; - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'start', '>= 0', errVal); diff --git a/lib/internal/crypto/pbkdf2.js b/lib/internal/crypto/pbkdf2.js index 2fc211a87d7635..e967e0a7945fc6 100644 --- a/lib/internal/crypto/pbkdf2.js +++ b/lib/internal/crypto/pbkdf2.js @@ -52,7 +52,10 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'iterations', 'number'); if (iterations < 0) - throw new errors.RangeError('ERR_OUT_OF_RANGE', 'iterations'); + throw new errors.RangeError('ERR_OUT_OF_RANGE', + 'iterations', + 'a non-negative number', + iterations); if (typeof keylen !== 'number') throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'keylen', 'number'); diff --git a/lib/internal/errors.js b/lib/internal/errors.js index c15c85b55429fb..9e3d1a9355e4f0 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -444,7 +444,7 @@ E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support'); E('ERR_NO_ICU', '%s is not supported on Node.js compiled without ICU'); E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported'); E('ERR_OUTOFMEMORY', 'Out of memory'); -E('ERR_OUT_OF_RANGE', 'The "%s" argument is out of range'); +E('ERR_OUT_OF_RANGE', outOfRange); E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s'); E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s'); E('ERR_SCRIPT_EXECUTION_INTERRUPTED', @@ -505,9 +505,6 @@ 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', (start, end, value) => { - return `The value of "${start}" must be ${end}. Received "${value}"`; -}); E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed'); E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed'); @@ -620,3 +617,10 @@ function invalidChar(name, field) { } return msg; } + +function outOfRange(name, range, value) { + let msg = `The value of "${name}" is out of range.`; + if (range) msg += ` It must be ${range}.`; + if (value !== undefined) msg += ` Received ${value}`; + return msg; +} diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 141f7fd0164531..e2ce4071d707f5 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -851,7 +851,8 @@ class Http2Session extends EventEmitter { if (typeof id !== 'number') throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'number'); if (id <= 0 || id > kMaxStreams) - throw new errors.RangeError('ERR_OUT_OF_RANGE'); + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'id', + `> 0 and <= ${kMaxStreams}`, id); this[kHandle].setNextStreamID(id); } diff --git a/lib/internal/timers.js b/lib/internal/timers.js index e4fbc6e0a363d4..e54162202102ff 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -115,7 +115,7 @@ function validateTimerDuration(msecs) { } if (msecs < 0 || !isFinite(msecs)) { - throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', + throw new errors.RangeError('ERR_OUT_OF_RANGE', 'msecs', 'a non-negative finite number', msecs); } diff --git a/test/parallel/test-child-process-spawnsync-validation-errors.js b/test/parallel/test-child-process-spawnsync-validation-errors.js index 7e38b549dc2e09..433e0fce167366 100644 --- a/test/parallel/test-child-process-spawnsync-validation-errors.js +++ b/test/parallel/test-child-process-spawnsync-validation-errors.js @@ -15,7 +15,7 @@ if (common.isWindows) { } const invalidRangeError = - common.expectsError({ code: 'ERR_VALUE_OUT_OF_RANGE', type: RangeError }, 20); + common.expectsError({ code: 'ERR_OUT_OF_RANGE', type: RangeError }, 20); function pass(option, value) { // Run the command with the specified option. Since it's not a real command, diff --git a/test/parallel/test-crypto-pbkdf2.js b/test/parallel/test-crypto-pbkdf2.js index af8acd42c7c7fd..3576b69301e317 100644 --- a/test/parallel/test-crypto-pbkdf2.js +++ b/test/parallel/test-crypto-pbkdf2.js @@ -70,7 +70,8 @@ common.expectsError( { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "iterations" argument is out of range' + message: 'The value of "iterations" is out of range. ' + + 'It must be a non-negative number. Received -1' } ); @@ -93,7 +94,7 @@ common.expectsError( }, { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "keylen" argument is out of range' + message: 'The value of "keylen" is out of range.' }); }); diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index 3926eb385044d9..a8f21cb4fe7036 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -291,7 +291,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "offset" argument is out of range' + message: 'The value of "offset" is out of range.' } ); @@ -300,7 +300,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "offset" argument is out of range' + message: 'The value of "offset" is out of range.' } ); @@ -309,7 +309,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "offset" argument is out of range' + message: 'The value of "offset" is out of range.' } ); @@ -318,7 +318,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "offset" argument is out of range' + message: 'The value of "offset" is out of range.' } ); @@ -421,7 +421,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "size" argument is out of range' + message: 'The value of "size" is out of range.' } ); @@ -430,7 +430,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "size" argument is out of range' + message: 'The value of "size" is out of range.' } ); @@ -439,7 +439,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "size" argument is out of range' + message: 'The value of "size" is out of range.' } ); @@ -448,7 +448,7 @@ process.setMaxListeners(256); { code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The "size" argument is out of range' + message: 'The value of "size" is out of range.' } ); diff --git a/test/parallel/test-event-emitter-max-listeners.js b/test/parallel/test-event-emitter-max-listeners.js index 319e0dffd6494d..a906000477f7da 100644 --- a/test/parallel/test-event-emitter-max-listeners.js +++ b/test/parallel/test-event-emitter-max-listeners.js @@ -36,8 +36,9 @@ for (const obj of throwsObjs) { () => e.setMaxListeners(obj), { code: 'ERR_OUT_OF_RANGE', - type: TypeError, - message: 'The "n" argument is out of range' + type: RangeError, + message: 'The value of "n" is out of range. ' + + `It must be a non-negative number. Received ${obj}` } ); @@ -45,8 +46,9 @@ for (const obj of throwsObjs) { () => events.defaultMaxListeners = obj, { code: 'ERR_OUT_OF_RANGE', - type: TypeError, - message: 'The "defaultMaxListeners" argument is out of range' + type: RangeError, + message: 'The value of "defaultMaxListeners" is out of range. ' + + `It must be a non-negative number. Received ${obj}` } ); } diff --git a/test/parallel/test-file-write-stream3.js b/test/parallel/test-file-write-stream3.js index cdc01e873389a2..4bf7b88ff1667f 100644 --- a/test/parallel/test-file-write-stream3.js +++ b/test/parallel/test-file-write-stream3.js @@ -180,8 +180,9 @@ const run_test_4 = common.mustCall(function() { fs.createWriteStream(filepath, { start: -5, flags: 'r+' }); }; const err = { - code: 'ERR_VALUE_OUT_OF_RANGE', - message: 'The value of "start" must be >= 0. Received "{start: -5}"', + code: 'ERR_OUT_OF_RANGE', + message: 'The value of "start" is out of range. ' + + 'It must be >= 0. Received {start: -5}', type: RangeError }; common.expectsError(block, err); diff --git a/test/parallel/test-fs-read-stream-inherit.js b/test/parallel/test-fs-read-stream-inherit.js index 6a83dd4eb4b459..f43f81ddee395d 100644 --- a/test/parallel/test-fs-read-stream-inherit.js +++ b/test/parallel/test-fs-read-stream-inherit.js @@ -110,14 +110,15 @@ const rangeFile = fixtures.path('x.txt'); { const message = - 'The value of "start" must be <= "end". Received "{start: 10, end: 2}"'; + 'The value of "start" is out of range. It must be <= "end". ' + + 'Received {start: 10, end: 2}'; common.expectsError( () => { fs.createReadStream(rangeFile, Object.create({ start: 10, end: 2 })); }, { - code: 'ERR_VALUE_OUT_OF_RANGE', + code: 'ERR_OUT_OF_RANGE', message, type: RangeError }); diff --git a/test/parallel/test-fs-read-stream.js b/test/parallel/test-fs-read-stream.js index ecc00edc851c55..1b9cc4aa90f523 100644 --- a/test/parallel/test-fs-read-stream.js +++ b/test/parallel/test-fs-read-stream.js @@ -145,9 +145,9 @@ common.expectsError( fs.createReadStream(rangeFile, { start: 10, end: 2 }); }, { - code: 'ERR_VALUE_OUT_OF_RANGE', - message: - 'The value of "start" must be <= "end". Received "{start: 10, end: 2}"', + code: 'ERR_OUT_OF_RANGE', + message: 'The value of "start" is out of range. It must be <= "end". ' + + 'Received {start: 10, end: 2}', type: RangeError }); diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index 78582a53504748..3673749224e248 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -281,8 +281,18 @@ assert.strictEqual( ); assert.strictEqual( - errors.message('ERR_VALUE_OUT_OF_RANGE', ['A', 'some values', 'B']), - 'The value of "A" must be some values. Received "B"' + errors.message('ERR_OUT_OF_RANGE', ['A']), + 'The value of "A" is out of range.' +); + +assert.strictEqual( + errors.message('ERR_OUT_OF_RANGE', ['A', 'some values']), + 'The value of "A" is out of range. It must be some values.' +); + +assert.strictEqual( + errors.message('ERR_OUT_OF_RANGE', ['A', 'some values', 'B']), + 'The value of "A" is out of range. It must be some values. Received B' ); assert.strictEqual( diff --git a/test/parallel/test-timers-enroll-invalid-msecs.js b/test/parallel/test-timers-enroll-invalid-msecs.js index df6ad04069910f..384ab0ab257908 100644 --- a/test/parallel/test-timers-enroll-invalid-msecs.js +++ b/test/parallel/test-timers-enroll-invalid-msecs.js @@ -27,10 +27,11 @@ const timers = require('timers'); common.expectsError( () => timers.enroll({}, val), { - code: 'ERR_VALUE_OUT_OF_RANGE', + code: 'ERR_OUT_OF_RANGE', type: RangeError, - message: 'The value of "msecs" must be a non-negative ' + - `finite number. Received "${val}"` + message: 'The value of "msecs" is out of range. ' + + 'It must be a non-negative finite number. ' + + `Received ${val}` } ); });