Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3573545

Browse filesBrowse 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
Copy full SHA for 3573545

File tree

Expand file treeCollapse file tree

2 files changed

+6
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+6
-3
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/random.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/random.js
+3-1Lines changed: 3 additions & 1 deletion
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)
Collapse file

‎test/parallel/test-crypto-random.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-random.js
+3-2Lines changed: 3 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.