Skip to content

Commit

Permalink
crypto: improve randomInt out-of-range error message
Browse files Browse the repository at this point in the history
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: nodejs#35088
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
Trott authored and joesepi committed Oct 22, 2020
1 parent 7b75cde commit 2f88f4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`
});
}

Expand Down

0 comments on commit 2f88f4b

Please sign in to comment.