| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ const { | |||
| 8 | 8 | Error, | |
| 9 | 9 | ErrorCaptureStackTrace, | |
| 10 | 10 | FunctionPrototypeCall, | |
| 11 | + FunctionPrototypeSymbolHasInstance, | ||
| 11 | 12 | NumberParseInt, | |
| 12 | 13 | ObjectDefineProperties, | |
| 13 | 14 | ObjectDefineProperty, | |
@@ -96,7 +97,7 @@ function isError(e) { | |||
| 96 | 97 | // An error could be an instance of Error while not being a native error | |
| 97 | 98 | // or could be from a different realm and not be instance of Error but still | |
| 98 | 99 | // be a native error. | |
| 99 | - return isNativeError(e) || e instanceof Error; | ||
| 100 | + return isNativeError(e) || FunctionPrototypeSymbolHasInstance(Error, e); | ||
| 100 | 101 | } | |
| 101 | 102 | ||
| 102 | 103 | // Keep a list of deprecation codes that have been warned on so we only warn on | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,6 +72,7 @@ const { | |||
| 72 | 72 | ObjectPrototype, | |
| 73 | 73 | ObjectPrototypeHasOwnProperty, | |
| 74 | 74 | ObjectPrototypePropertyIsEnumerable, | |
| 75 | + ObjectPrototypeToString, | ||
| 75 | 76 | ObjectSeal, | |
| 76 | 77 | ObjectSetPrototypeOf, | |
| 77 | 78 | Promise, | |
@@ -1468,13 +1469,19 @@ function getDuplicateErrorFrameRanges(frames) { | |||
| 1468 | 1469 | } | |
| 1469 | 1470 | ||
| 1470 | 1471 | function getStackString(ctx, error) { | |
| 1471 | - if (error.stack) { | ||
| 1472 | - if (typeof error.stack === 'string') { | ||
| 1473 | - return error.stack; | ||
| 1472 | + let stack; | ||
| 1473 | + try { | ||
| 1474 | + stack = error.stack; | ||
| 1475 | + } catch { | ||
| 1476 | + // If stack is getter that throws, we ignore the error. | ||
| 1477 | + } | ||
| 1478 | + if (stack) { | ||
| 1479 | + if (typeof stack === 'string') { | ||
| 1480 | + return stack; | ||
| 1474 | 1481 | } | |
| 1475 | 1482 | ctx.seen.push(error); | |
| 1476 | 1483 | ctx.indentationLvl += 4; | |
| 1477 | - const result = formatValue(ctx, error.stack); | ||
| 1484 | + const result = formatValue(ctx, stack); | ||
| 1478 | 1485 | ctx.indentationLvl -= 4; | |
| 1479 | 1486 | ctx.seen.pop(); | |
| 1480 | 1487 | return `${ErrorPrototypeToString(error)}\n ${result}`; | |
@@ -1570,18 +1577,6 @@ function improveStack(stack, constructor, name, tag) { | |||
| 1570 | 1577 | return stack; | |
| 1571 | 1578 | } | |
| 1572 | 1579 | ||
| 1573 | - function removeDuplicateErrorKeys(ctx, keys, err, stack) { | ||
| 1574 | - if (!ctx.showHidden && keys.length !== 0) { | ||
| 1575 | - for (const name of ['name', 'message', 'stack']) { | ||
| 1576 | - const index = ArrayPrototypeIndexOf(keys, name); | ||
| 1577 | - // Only hide the property if it's a string and if it's part of the original stack | ||
| 1578 | - if (index !== -1 && (typeof err[name] !== 'string' || StringPrototypeIncludes(stack, err[name]))) { | ||
| 1579 | - ArrayPrototypeSplice(keys, index, 1); | ||
| 1580 | - } | ||
| 1581 | - } | ||
| 1582 | - } | ||
| 1583 | - } | ||
| 1584 | - | ||
| 1585 | 1580 | function markNodeModules(ctx, line) { | |
| 1586 | 1581 | let tempLine = ''; | |
| 1587 | 1582 | let lastPos = 0; | |
@@ -1664,28 +1659,72 @@ function safeGetCWD() { | |||
| 1664 | 1659 | } | |
| 1665 | 1660 | ||
| 1666 | 1661 | function formatError(err, constructor, tag, ctx, keys) { | |
| 1667 | - const name = err.name != null ? err.name : 'Error'; | ||
| 1668 | - let stack = getStackString(ctx, err); | ||
| 1662 | + let message, name, stack; | ||
| 1663 | + try { | ||
| 1664 | + stack = getStackString(ctx, err); | ||
| 1665 | + } catch { | ||
| 1666 | + return ObjectPrototypeToString(err); | ||
| 1667 | + } | ||
| 1669 | 1668 | ||
| 1670 | - removeDuplicateErrorKeys(ctx, keys, err, stack); | ||
| 1669 | + let messageIsGetterThatThrows = false; | ||
| 1670 | + try { | ||
| 1671 | + message = err.message; | ||
| 1672 | + } catch { | ||
| 1673 | + messageIsGetterThatThrows = true; | ||
| 1674 | + } | ||
| 1675 | + let nameIsGetterThatThrows = false; | ||
| 1676 | + try { | ||
| 1677 | + name = err.name; | ||
| 1678 | + } catch { | ||
| 1679 | + nameIsGetterThatThrows = true; | ||
| 1680 | + } | ||
| 1681 | + | ||
| 1682 | + if (!ctx.showHidden && keys.length !== 0) { | ||
| 1683 | + const index = ArrayPrototypeIndexOf(keys, 'stack'); | ||
| 1684 | + if (index !== -1) { | ||
| 1685 | + ArrayPrototypeSplice(keys, index, 1); | ||
| 1686 | + } | ||
| 1687 | + | ||
| 1688 | + if (!messageIsGetterThatThrows) { | ||
| 1689 | + const index = ArrayPrototypeIndexOf(keys, 'message'); | ||
| 1690 | + // Only hide the property if it's a string and if it's part of the original stack | ||
| 1691 | + if (index !== -1 && (typeof message !== 'string' || StringPrototypeIncludes(stack, message))) { | ||
| 1692 | + ArrayPrototypeSplice(keys, index, 1); | ||
| 1693 | + } | ||
| 1694 | + } | ||
| 1695 | + | ||
| 1696 | + if (!nameIsGetterThatThrows) { | ||
| 1697 | + const index = ArrayPrototypeIndexOf(keys, 'name'); | ||
| 1698 | + // Only hide the property if it's a string and if it's part of the original stack | ||
| 1699 | + if (index !== -1 && (typeof name !== 'string' || StringPrototypeIncludes(stack, name))) { | ||
| 1700 | + ArrayPrototypeSplice(keys, index, 1); | ||
| 1701 | + } | ||
| 1702 | + } | ||
| 1703 | + } | ||
| 1704 | + name ??= 'Error'; | ||
| 1671 | 1705 | ||
| 1672 | 1706 | if ('cause' in err && | |
| 1673 | 1707 | (keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) { | |
| 1674 | 1708 | ArrayPrototypePush(keys, 'cause'); | |
| 1675 | 1709 | } | |
| 1676 | 1710 | ||
| 1677 | 1711 | // Print errors aggregated into AggregateError | |
| 1678 | - if (ArrayIsArray(err.errors) && | ||
| 1712 | + try { | ||
| 1713 | + const errors = err.errors; | ||
| 1714 | + if (ArrayIsArray(errors) && | ||
| 1679 | 1715 | (keys.length === 0 || !ArrayPrototypeIncludes(keys, 'errors'))) { | |
| 1680 | - ArrayPrototypePush(keys, 'errors'); | ||
| 1716 | + ArrayPrototypePush(keys, 'errors'); | ||
| 1717 | + } | ||
| 1718 | + } catch { | ||
| 1719 | + // If errors is a getter that throws, we ignore the error. | ||
| 1681 | 1720 | } | |
| 1682 | 1721 | ||
| 1683 | 1722 | stack = improveStack(stack, constructor, name, tag); | |
| 1684 | 1723 | ||
| 1685 | 1724 | // Ignore the error message if it's contained in the stack. | |
| 1686 | - let pos = (err.message && StringPrototypeIndexOf(stack, err.message)) || -1; | ||
| 1725 | + let pos = (message && StringPrototypeIndexOf(stack, message)) || -1; | ||
| 1687 | 1726 | if (pos !== -1) | |
| 1688 | - pos += err.message.length; | ||
| 1727 | + pos += message.length; | ||
| 1689 | 1728 | // Wrap the error in brackets in case it has no stack trace. | |
| 1690 | 1729 | const stackStart = StringPrototypeIndexOf(stack, '\n at', pos); | |
| 1691 | 1730 | if (stackStart === -1) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3716,3 +3716,28 @@ ${error.stack.split('\n').slice(1).join('\n')}`, | |||
| 3716 | 3716 | ||
| 3717 | 3717 | assert.strictEqual(inspect(error), '[Error: foo\n [Error: bar\n [Circular *1]]]'); | |
| 3718 | 3718 | } | |
| 3719 | + | ||
| 3720 | + { | ||
| 3721 | + Object.defineProperty(Error, Symbol.hasInstance, | ||
| 3722 | + { __proto__: null, value: common.mustNotCall(), configurable: true }); | ||
| 3723 | + const error = new Error(); | ||
| 3724 | + | ||
| 3725 | + const throwingGetter = { | ||
| 3726 | + __proto__: null, | ||
| 3727 | + get() { | ||
| 3728 | + throw error; | ||
| 3729 | + }, | ||
| 3730 | + configurable: true, | ||
| 3731 | + enumerable: true, | ||
| 3732 | + }; | ||
| 3733 | + | ||
| 3734 | + Object.defineProperties(error, { | ||
| 3735 | + name: throwingGetter, | ||
| 3736 | + stack: throwingGetter, | ||
| 3737 | + cause: throwingGetter, | ||
| 3738 | + }); | ||
| 3739 | + | ||
| 3740 | + assert.strictEqual(inspect(error), `[object Error] {\n stack: [Getter/Setter],\n name: [Getter],\n cause: [Getter]\n}`); | ||
| 3741 | + assert.match(inspect(DOMException.prototype), /^\[object DOMException\] \{/); | ||
| 3742 | + delete Error[Symbol.hasInstance]; | ||
| 3743 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments