Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: improve randomInt out-of-range error message #35088

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 > ' +
Trott marked this conversation as resolved.
Show resolved Hide resolved
`${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