Skip to content

Commit 05fe291

Browse files
committed
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: #17603
1 parent 0b78895 commit 05fe291

17 files changed

+70
-42
lines changed

doc/api/errors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ An operation caused an out-of-memory condition.
13401340
<a id="ERR_OUT_OF_RANGE"></a>
13411341
### ERR_OUT_OF_RANGE
13421342

1343-
An input argument value was outside an acceptable range.
1343+
A given value is out of the accepted range.
13441344

13451345
<a id="ERR_PARSE_HISTORY_DATA"></a>
13461346
### ERR_PARSE_HISTORY_DATA
@@ -1597,7 +1597,7 @@ entry types were found.
15971597
<a id="ERR_VALUE_OUT_OF_RANGE"></a>
15981598
### ERR_VALUE_OUT_OF_RANGE
15991599

1600-
A given value is out of the accepted range.
1600+
Superseded by `ERR_OUT_OF_RANGE`
16011601

16021602
<a id="ERR_ZLIB_BINDING_CLOSED"></a>
16031603
### ERR_ZLIB_BINDING_CLOSED

lib/child_process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ exports.execSync = execSync;
650650

651651
function validateTimeout(timeout) {
652652
if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) {
653-
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE',
653+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
654654
'timeout',
655655
'an unsigned integer',
656656
timeout);
@@ -660,7 +660,7 @@ function validateTimeout(timeout) {
660660

661661
function validateMaxBuffer(maxBuffer) {
662662
if (maxBuffer != null && !(typeof maxBuffer === 'number' && maxBuffer >= 0)) {
663-
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE',
663+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
664664
'options.maxBuffer',
665665
'a positive number',
666666
maxBuffer);

lib/events.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
5656
// greater and not a NaN).
5757
if (typeof arg !== 'number' || arg < 0 || arg !== arg) {
5858
const errors = lazyErrors();
59-
throw new errors.TypeError('ERR_OUT_OF_RANGE', 'defaultMaxListeners');
59+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
60+
'defaultMaxListeners',
61+
'a non-negative number',
62+
arg);
6063
}
6164
defaultMaxListeners = arg;
6265
}
@@ -78,7 +81,8 @@ EventEmitter.init = function() {
7881
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
7982
if (typeof n !== 'number' || n < 0 || isNaN(n)) {
8083
const errors = lazyErrors();
81-
throw new errors.TypeError('ERR_OUT_OF_RANGE', 'n');
84+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'n',
85+
'a non-negative number', n);
8286
}
8387
this._maxListeners = n;
8488
return this;

lib/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,7 +2353,7 @@ function ReadStream(path, options) {
23532353

23542354
if (this.start > this.end) {
23552355
const errVal = `{start: ${this.start}, end: ${this.end}}`;
2356-
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE',
2356+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
23572357
'start',
23582358
'<= "end"',
23592359
errVal);
@@ -2511,7 +2511,7 @@ function WriteStream(path, options) {
25112511
}
25122512
if (this.start < 0) {
25132513
const errVal = `{start: ${this.start}}`;
2514-
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE',
2514+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
25152515
'start',
25162516
'>= 0',
25172517
errVal);

lib/internal/crypto/pbkdf2.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
5252
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'iterations', 'number');
5353

5454
if (iterations < 0)
55-
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'iterations');
55+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
56+
'iterations',
57+
'a non-negative number',
58+
iterations);
5659

5760
if (typeof keylen !== 'number')
5861
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'keylen', 'number');

lib/internal/errors.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
444444
E('ERR_NO_ICU', '%s is not supported on Node.js compiled without ICU');
445445
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported');
446446
E('ERR_OUTOFMEMORY', 'Out of memory');
447-
E('ERR_OUT_OF_RANGE', 'The "%s" argument is out of range');
447+
E('ERR_OUT_OF_RANGE', outOfRange);
448448
E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
449449
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s');
450450
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
@@ -502,9 +502,6 @@ E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' +
502502
'See https://github.com/nodejs/node/wiki/Intl');
503503
E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
504504
'At least one valid performance entry type is required');
505-
E('ERR_VALUE_OUT_OF_RANGE', (start, end, value) => {
506-
return `The value of "${start}" must be ${end}. Received "${value}"`;
507-
});
508505
E('ERR_ZLIB_BINDING_CLOSED', 'zlib binding closed');
509506
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed');
510507

@@ -617,3 +614,10 @@ function invalidChar(name, field) {
617614
}
618615
return msg;
619616
}
617+
618+
function outOfRange(name, range, value) {
619+
let msg = `The value of "${name}" is out of range.`;
620+
if (range) msg += ` It must be ${range}.`;
621+
if (value !== undefined) msg += ` Received ${value}`;
622+
return msg;
623+
}

lib/internal/http2/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,8 @@ class Http2Session extends EventEmitter {
851851
if (typeof id !== 'number')
852852
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'number');
853853
if (id <= 0 || id > kMaxStreams)
854-
throw new errors.RangeError('ERR_OUT_OF_RANGE');
854+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'id',
855+
`> 0 and <= ${kMaxStreams}`, id);
855856
this[kHandle].setNextStreamID(id);
856857
}
857858

lib/internal/timers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function validateTimerDuration(msecs) {
115115
}
116116

117117
if (msecs < 0 || !isFinite(msecs)) {
118-
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
118+
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'msecs',
119119
'a non-negative finite number', msecs);
120120
}
121121

test/parallel/test-child-process-spawnsync-validation-errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (common.isWindows) {
1515
}
1616

1717
const invalidRangeError =
18-
common.expectsError({ code: 'ERR_VALUE_OUT_OF_RANGE', type: RangeError }, 20);
18+
common.expectsError({ code: 'ERR_OUT_OF_RANGE', type: RangeError }, 20);
1919

2020
function pass(option, value) {
2121
// Run the command with the specified option. Since it's not a real command,

test/parallel/test-crypto-pbkdf2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ common.expectsError(
7070
{
7171
code: 'ERR_OUT_OF_RANGE',
7272
type: RangeError,
73-
message: 'The "iterations" argument is out of range'
73+
message: 'The value of "iterations" is out of range. ' +
74+
'It must be a non-negative number. Received -1'
7475
}
7576
);
7677

@@ -93,7 +94,7 @@ common.expectsError(
9394
}, {
9495
code: 'ERR_OUT_OF_RANGE',
9596
type: RangeError,
96-
message: 'The "keylen" argument is out of range'
97+
message: 'The value of "keylen" is out of range.'
9798
});
9899
});
99100

0 commit comments

Comments
 (0)