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: https://github.com/nodejs/node/pull/35088
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Rich Trott 2020-09-07 07:36:07 -07:00
parent 1204400d64
commit d7d0fab70e
2 changed files with 6 additions and 3 deletions

View File

@ -150,7 +150,9 @@ function randomInt(min, max, callback) {
throw new ERR_INVALID_ARG_TYPE('max', 'safe integer', max);
}
if (max <= min) {
throw new ERR_OUT_OF_RANGE('max', `> ${min}`, max);
throw new ERR_OUT_OF_RANGE(
'max', `greater than the value of "min" (${min})`, max
);
}
// First we generate a random int between [0..range)

View File

@ -462,8 +462,9 @@ assert.throws(
assert.throws(() => crypto.randomInt(...arg, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "max" is out of range. It must be > ' +
`${arg[arg.length - 2] || 0}. Received ${arg[arg.length - 1]}`
message: 'The value of "max" is out of range. It must be greater than ' +
`the value of "min" (${arg[arg.length - 2] || 0}). ` +
`Received ${arg[arg.length - 1]}`
});
}