@@ -72,6 +72,7 @@ const {
7272 ObjectPrototype,
7373 ObjectPrototypeHasOwnProperty,
7474 ObjectPrototypePropertyIsEnumerable,
75+ ObjectPrototypeToString,
7576 ObjectSeal,
7677 ObjectSetPrototypeOf,
7778 Promise,
@@ -1703,13 +1704,19 @@ function getDuplicateErrorFrameRanges(frames) {
17031704}
17041705
17051706function getStackString ( ctx , error ) {
1706- if ( error . stack ) {
1707- if ( typeof error . stack === 'string' ) {
1708- return error . stack ;
1707+ let stack ;
1708+ try {
1709+ stack = error . stack ;
1710+ } catch {
1711+ // If stack is getter that throws, we ignore the error.
1712+ }
1713+ if ( stack ) {
1714+ if ( typeof stack === 'string' ) {
1715+ return stack ;
17091716 }
17101717 ctx . seen . push ( error ) ;
17111718 ctx . indentationLvl += 4 ;
1712- const result = formatValue ( ctx , error . stack ) ;
1719+ const result = formatValue ( ctx , stack ) ;
17131720 ctx . indentationLvl -= 4 ;
17141721 ctx . seen . pop ( ) ;
17151722 return `${ ErrorPrototypeToString ( error ) } \n ${ result } ` ;
@@ -1805,18 +1812,6 @@ function improveStack(stack, constructor, name, tag) {
18051812 return stack ;
18061813}
18071814
1808- function removeDuplicateErrorKeys ( ctx , keys , err , stack ) {
1809- if ( ! ctx . showHidden && keys . length !== 0 ) {
1810- for ( const name of [ 'name' , 'message' , 'stack' ] ) {
1811- const index = ArrayPrototypeIndexOf ( keys , name ) ;
1812- // Only hide the property if it's a string and if it's part of the original stack
1813- if ( index !== - 1 && ( typeof err [ name ] !== 'string' || StringPrototypeIncludes ( stack , err [ name ] ) ) ) {
1814- ArrayPrototypeSplice ( keys , index , 1 ) ;
1815- }
1816- }
1817- }
1818- }
1819-
18201815function markNodeModules ( ctx , line ) {
18211816 let tempLine = '' ;
18221817 let lastPos = 0 ;
@@ -1899,28 +1894,67 @@ function safeGetCWD() {
18991894}
19001895
19011896function formatError ( err , constructor , tag , ctx , keys ) {
1902- const name = err . name != null ? err . name : 'Error' ;
1903- let stack = getStackString ( ctx , err ) ;
1904-
1905- removeDuplicateErrorKeys ( ctx , keys , err , stack ) ;
1897+ let message , name , stack ;
1898+ try {
1899+ stack = getStackString ( ctx , err ) ;
1900+ if ( ! ctx . showHidden && keys . length !== 0 ) {
1901+ const index = ArrayPrototypeIndexOf ( keys , 'stack' ) ;
1902+ // Only hide the property if it's a string and if it's part of the original stack
1903+ if ( index !== - 1 ) {
1904+ ArrayPrototypeSplice ( keys , index , 1 ) ;
1905+ }
1906+ }
1907+ } catch {
1908+ return ObjectPrototypeToString ( err ) ;
1909+ }
1910+ try {
1911+ message = err . message ;
1912+ if ( ! ctx . showHidden && keys . length !== 0 ) {
1913+ const index = ArrayPrototypeIndexOf ( keys , 'message' ) ;
1914+ // Only hide the property if it's a string and if it's part of the original stack
1915+ if ( index !== - 1 && ( typeof message !== 'string' || StringPrototypeIncludes ( stack , message ) ) ) {
1916+ ArrayPrototypeSplice ( keys , index , 1 ) ;
1917+ }
1918+ }
1919+ } catch {
1920+ // If message is a getter that throws, we ignore the error.
1921+ }
1922+ try {
1923+ name = err . name ;
1924+ if ( ! ctx . showHidden && keys . length !== 0 ) {
1925+ const index = ArrayPrototypeIndexOf ( keys , 'name' ) ;
1926+ // Only hide the property if it's a string and if it's part of the original stack
1927+ if ( index !== - 1 && ( typeof name !== 'string' || StringPrototypeIncludes ( stack , name ) ) ) {
1928+ ArrayPrototypeSplice ( keys , index , 1 ) ;
1929+ }
1930+ }
1931+ name ??= 'Error' ;
1932+ } catch {
1933+ name = 'Error' ;
1934+ }
19061935
19071936 if ( 'cause' in err &&
19081937 ( keys . length === 0 || ! ArrayPrototypeIncludes ( keys , 'cause' ) ) ) {
19091938 ArrayPrototypePush ( keys , 'cause' ) ;
19101939 }
19111940
19121941 // Print errors aggregated into AggregateError
1913- if ( ArrayIsArray ( err . errors ) &&
1942+ try {
1943+ const errors = err . errors ;
1944+ if ( ArrayIsArray ( errors ) &&
19141945 ( keys . length === 0 || ! ArrayPrototypeIncludes ( keys , 'errors' ) ) ) {
1915- ArrayPrototypePush ( keys , 'errors' ) ;
1946+ ArrayPrototypePush ( keys , 'errors' ) ;
1947+ }
1948+ } catch {
1949+ // If errors is a getter that throws, we ignore the error.
19161950 }
19171951
19181952 stack = improveStack ( stack , constructor , name , tag ) ;
19191953
19201954 // Ignore the error message if it's contained in the stack.
1921- let pos = ( err . message && StringPrototypeIndexOf ( stack , err . message ) ) || - 1 ;
1955+ let pos = ( message && StringPrototypeIndexOf ( stack , message ) ) || - 1 ;
19221956 if ( pos !== - 1 )
1923- pos += err . message . length ;
1957+ pos += message . length ;
19241958 // Wrap the error in brackets in case it has no stack trace.
19251959 const stackStart = StringPrototypeIndexOf ( stack , '\n at' , pos ) ;
19261960 if ( stackStart === - 1 ) {
0 commit comments