From 2f88f4ba1cc87360c1f6ab970fb37ae35c73f081 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 7 Sep 2020 07:36:07 -0700 Subject: [PATCH] crypto: improve randomInt out-of-range error message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Tobias Nießen --- lib/internal/crypto/random.js | 4 +++- test/parallel/test-crypto-random.js | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 5f7f7efbf5b7c3..99d4a2bd89f8dd 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -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) diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index b3f14013e59a33..8864b179871137 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -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]}` }); }