|
10 | 10 | // value statically and permanently identifies the error. While the error
|
11 | 11 | // message may change, the code should not.
|
12 | 12 |
|
13 |
| -const { Object } = primordials; |
| 13 | +const { Object, Math } = primordials; |
14 | 14 |
|
15 | 15 | const kCode = Symbol('code');
|
16 | 16 | const kInfo = Symbol('info');
|
@@ -574,6 +574,17 @@ function oneOf(expected, thing) {
|
574 | 574 | }
|
575 | 575 | }
|
576 | 576 |
|
| 577 | +// Only use this for integers! Decimal numbers do not work with this function. |
| 578 | +function addNumericalSeparator(val) { |
| 579 | + let res = ''; |
| 580 | + let i = val.length; |
| 581 | + const start = val[0] === '-' ? 1 : 0; |
| 582 | + for (; i >= start + 4; i -= 3) { |
| 583 | + res = `_${val.slice(i - 3, i)}${res}`; |
| 584 | + } |
| 585 | + return `${val.slice(0, i)}${res}`; |
| 586 | +} |
| 587 | + |
577 | 588 | module.exports = {
|
578 | 589 | addCodeToName, // Exported for NghttpError
|
579 | 590 | codes,
|
@@ -990,7 +1001,20 @@ E('ERR_OUT_OF_RANGE',
|
990 | 1001 | assert(range, 'Missing "range" argument');
|
991 | 1002 | let msg = replaceDefaultBoolean ? str :
|
992 | 1003 | `The value of "${str}" is out of range.`;
|
993 |
| - msg += ` It must be ${range}. Received ${input}`; |
| 1004 | + let received; |
| 1005 | + if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) { |
| 1006 | + received = addNumericalSeparator(String(input)); |
| 1007 | + // eslint-disable-next-line valid-typeof |
| 1008 | + } else if (typeof input === 'bigint') { |
| 1009 | + received = String(input); |
| 1010 | + if (input > 2n ** 32n || input < -(2n ** 32n)) { |
| 1011 | + received = addNumericalSeparator(received); |
| 1012 | + } |
| 1013 | + received += 'n'; |
| 1014 | + } else { |
| 1015 | + received = lazyInternalUtilInspect().inspect(input); |
| 1016 | + } |
| 1017 | + msg += ` It must be ${range}. Received ${received}`; |
994 | 1018 | return msg;
|
995 | 1019 | }, RangeError);
|
996 | 1020 | E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
|
|
0 commit comments