@@ -72,6 +72,7 @@ const {
7272 ObjectPrototype,
7373 ObjectPrototypeHasOwnProperty,
7474 ObjectPrototypePropertyIsEnumerable,
75+ ObjectPrototypeToString,
7576 ObjectSeal,
7677 ObjectSetPrototypeOf,
7778 Promise,
@@ -1720,13 +1721,19 @@ function getDuplicateErrorFrameRanges(frames) {
17201721}
17211722
17221723function getStackString ( ctx , error ) {
1723- if ( error . stack ) {
1724- if ( typeof error . stack === 'string' ) {
1725- return error . stack ;
1724+ let stack ;
1725+ try {
1726+ stack = error . stack ;
1727+ } catch {
1728+ // If stack is getter that throws, we ignore the error.
1729+ }
1730+ if ( stack ) {
1731+ if ( typeof stack === 'string' ) {
1732+ return stack ;
17261733 }
17271734 ctx . seen . push ( error ) ;
17281735 ctx . indentationLvl += 4 ;
1729- const result = formatValue ( ctx , error . stack ) ;
1736+ const result = formatValue ( ctx , stack ) ;
17301737 ctx . indentationLvl -= 4 ;
17311738 ctx . seen . pop ( ) ;
17321739 return `${ ErrorPrototypeToString ( error ) } \n ${ result } ` ;
@@ -1822,18 +1829,6 @@ function improveStack(stack, constructor, name, tag) {
18221829 return stack ;
18231830}
18241831
1825- function removeDuplicateErrorKeys ( ctx , keys , err , stack ) {
1826- if ( ! ctx . showHidden && keys . length !== 0 ) {
1827- for ( const name of [ 'name' , 'message' , 'stack' ] ) {
1828- const index = ArrayPrototypeIndexOf ( keys , name ) ;
1829- // Only hide the property if it's a string and if it's part of the original stack
1830- if ( index !== - 1 && ( typeof err [ name ] !== 'string' || StringPrototypeIncludes ( stack , err [ name ] ) ) ) {
1831- ArrayPrototypeSplice ( keys , index , 1 ) ;
1832- }
1833- }
1834- }
1835- }
1836-
18371832function markNodeModules ( ctx , line ) {
18381833 let tempLine = '' ;
18391834 let lastPos = 0 ;
@@ -1916,28 +1911,67 @@ function safeGetCWD() {
19161911}
19171912
19181913function formatError ( err , constructor , tag , ctx , keys ) {
1919- const name = err . name != null ? err . name : 'Error' ;
1920- let stack = getStackString ( ctx , err ) ;
1921-
1922- removeDuplicateErrorKeys ( ctx , keys , err , stack ) ;
1914+ let message , name , stack ;
1915+ try {
1916+ stack = getStackString ( ctx , err ) ;
1917+ if ( ! ctx . showHidden && keys . length !== 0 ) {
1918+ const index = ArrayPrototypeIndexOf ( keys , 'stack' ) ;
1919+ // Only hide the property if it's a string and if it's part of the original stack
1920+ if ( index !== - 1 ) {
1921+ ArrayPrototypeSplice ( keys , index , 1 ) ;
1922+ }
1923+ }
1924+ } catch {
1925+ return ObjectPrototypeToString ( err ) ;
1926+ }
1927+ try {
1928+ message = err . message ;
1929+ if ( ! ctx . showHidden && keys . length !== 0 ) {
1930+ const index = ArrayPrototypeIndexOf ( keys , 'message' ) ;
1931+ // Only hide the property if it's a string and if it's part of the original stack
1932+ if ( index !== - 1 && ( typeof message !== 'string' || StringPrototypeIncludes ( stack , message ) ) ) {
1933+ ArrayPrototypeSplice ( keys , index , 1 ) ;
1934+ }
1935+ }
1936+ } catch {
1937+ // If message is a getter that throws, we ignore the error.
1938+ }
1939+ try {
1940+ name = err . name ;
1941+ if ( ! ctx . showHidden && keys . length !== 0 ) {
1942+ const index = ArrayPrototypeIndexOf ( keys , 'name' ) ;
1943+ // Only hide the property if it's a string and if it's part of the original stack
1944+ if ( index !== - 1 && ( typeof name !== 'string' || StringPrototypeIncludes ( stack , name ) ) ) {
1945+ ArrayPrototypeSplice ( keys , index , 1 ) ;
1946+ }
1947+ }
1948+ name ??= 'Error' ;
1949+ } catch {
1950+ name = 'Error' ;
1951+ }
19231952
19241953 if ( 'cause' in err &&
19251954 ( keys . length === 0 || ! ArrayPrototypeIncludes ( keys , 'cause' ) ) ) {
19261955 ArrayPrototypePush ( keys , 'cause' ) ;
19271956 }
19281957
19291958 // Print errors aggregated into AggregateError
1930- if ( ArrayIsArray ( err . errors ) &&
1959+ try {
1960+ const errors = err . errors ;
1961+ if ( ArrayIsArray ( errors ) &&
19311962 ( keys . length === 0 || ! ArrayPrototypeIncludes ( keys , 'errors' ) ) ) {
1932- ArrayPrototypePush ( keys , 'errors' ) ;
1963+ ArrayPrototypePush ( keys , 'errors' ) ;
1964+ }
1965+ } catch {
1966+ // If errors is a getter that throws, we ignore the error.
19331967 }
19341968
19351969 stack = improveStack ( stack , constructor , name , tag ) ;
19361970
19371971 // Ignore the error message if it's contained in the stack.
1938- let pos = ( err . message && StringPrototypeIndexOf ( stack , err . message ) ) || - 1 ;
1972+ let pos = ( message && StringPrototypeIndexOf ( stack , message ) ) || - 1 ;
19391973 if ( pos !== - 1 )
1940- pos += err . message . length ;
1974+ pos += message . length ;
19411975 // Wrap the error in brackets in case it has no stack trace.
19421976 const stackStart = StringPrototypeIndexOf ( stack , '\n at' , pos ) ;
19431977 if ( stackStart === - 1 ) {
0 commit comments