Skip to content

Commit 3573545

Browse files
Trottruyadorno
authored andcommitted
crypto: improve randomInt out-of-range error message
Previously, the crypto.randomInt() message when "max" was less than or equal to "min" made it sound like the lower bound for "max" was hard-coded. Make it clear that it is instead dynamic based on the value of "min". For crypto.randomInt(10,0): Before: RangeError [ERR_OUT_OF_RANGE]: The value of "max" is out of range. It must be > 10. Received 0 After: RangeError [ERR_OUT_OF_RANGE]: The value of "max" is out of range. It must be greater than the value of "min" (10). Received 0 PR-URL: #35088 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent bc38485 commit 3573545

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

lib/internal/crypto/random.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ function randomInt(min, max, callback) {
150150
throw new ERR_INVALID_ARG_TYPE('max', 'safe integer', max);
151151
}
152152
if (max <= min) {
153-
throw new ERR_OUT_OF_RANGE('max', `> ${min}`, max);
153+
throw new ERR_OUT_OF_RANGE(
154+
'max', `greater than the value of "min" (${min})`, max
155+
);
154156
}
155157

156158
// First we generate a random int between [0..range)

test/parallel/test-crypto-random.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,9 @@ assert.throws(
462462
assert.throws(() => crypto.randomInt(...arg, common.mustNotCall()), {
463463
code: 'ERR_OUT_OF_RANGE',
464464
name: 'RangeError',
465-
message: 'The value of "max" is out of range. It must be > ' +
466-
`${arg[arg.length - 2] || 0}. Received ${arg[arg.length - 1]}`
465+
message: 'The value of "max" is out of range. It must be greater than ' +
466+
`the value of "min" (${arg[arg.length - 2] || 0}). ` +
467+
`Received ${arg[arg.length - 1]}`
467468
});
468469
}
469470

0 commit comments

Comments
 (0)