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: #35088
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
Trott authored and ruyadorno committed Sep 17, 2020
1 parent bc38485 commit 3573545
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 3573545

Please sign in to comment.