|
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 { Math } = primordials; |
| 14 | + |
13 | 15 | const kCode = Symbol('code'); |
14 | 16 | const kInfo = Symbol('info'); |
15 | 17 | const messages = new Map(); |
@@ -573,6 +575,17 @@ function oneOf(expected, thing) { |
573 | 575 | } |
574 | 576 | } |
575 | 577 |
|
| 578 | +// Only use this for integers! Decimal numbers do not work with this function. |
| 579 | +function addNumericalSeparator(val) { |
| 580 | + let res = ''; |
| 581 | + let i = val.length; |
| 582 | + const start = val[0] === '-' ? 1 : 0; |
| 583 | + for (; i >= start + 4; i -= 3) { |
| 584 | + res = `_${val.slice(i - 3, i)}${res}`; |
| 585 | + } |
| 586 | + return `${val.slice(0, i)}${res}`; |
| 587 | +} |
| 588 | + |
576 | 589 | module.exports = { |
577 | 590 | addCodeToName, // Exported for NghttpError |
578 | 591 | codes, |
@@ -998,7 +1011,20 @@ E('ERR_OUT_OF_RANGE', |
998 | 1011 | assert(range, 'Missing "range" argument'); |
999 | 1012 | let msg = replaceDefaultBoolean ? str : |
1000 | 1013 | `The value of "${str}" is out of range.`; |
1001 | | - msg += ` It must be ${range}. Received ${input}`; |
| 1014 | + let received; |
| 1015 | + if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) { |
| 1016 | + received = addNumericalSeparator(String(input)); |
| 1017 | + // eslint-disable-next-line valid-typeof |
| 1018 | + } else if (typeof input === 'bigint') { |
| 1019 | + received = String(input); |
| 1020 | + if (input > 2n ** 32n || input < -(2n ** 32n)) { |
| 1021 | + received = addNumericalSeparator(received); |
| 1022 | + } |
| 1023 | + received += 'n'; |
| 1024 | + } else { |
| 1025 | + received = lazyInternalUtilInspect().inspect(input); |
| 1026 | + } |
| 1027 | + msg += ` It must be ${range}. Received ${received}`; |
1002 | 1028 | return msg; |
1003 | 1029 | }, RangeError); |
1004 | 1030 | E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error); |
|
0 commit comments