@@ -88,6 +88,7 @@ const {
8888 StringPrototypePadEnd,
8989 StringPrototypePadStart,
9090 StringPrototypeRepeat,
91+ StringPrototypeReplace,
9192 StringPrototypeReplaceAll,
9293 StringPrototypeSlice,
9394 StringPrototypeSplit,
@@ -733,6 +734,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
733734 } while ( ++ depth !== 3 ) ;
734735}
735736
737+ /** @type {(constructor: string, tag: string, fallback: string, size?: string) => string } */
736738function getPrefix ( constructor , tag , fallback , size = '' ) {
737739 if ( constructor === null ) {
738740 if ( tag !== '' && fallback !== tag ) {
@@ -1316,11 +1318,20 @@ function getStackFrames(ctx, err, stack) {
13161318 return frames ;
13171319}
13181320
1321+ /** @type {(stack: string, constructor: string | null, name: unknown, tag: string) => string } */
13191322function improveStack ( stack , constructor , name , tag ) {
13201323 // A stack trace may contain arbitrary data. Only manipulate the output
13211324 // for "regular errors" (errors that "look normal") for now.
13221325 let len = name . length ;
13231326
1327+ if ( typeof name !== 'string' ) {
1328+ stack = StringPrototypeReplace (
1329+ stack ,
1330+ `${ name } ` ,
1331+ `${ name } [${ StringPrototypeSlice ( getPrefix ( constructor , tag , 'Error' ) , 0 , - 1 ) } ]` ,
1332+ ) ;
1333+ }
1334+
13241335 if ( constructor === null ||
13251336 ( StringPrototypeEndsWith ( name , 'Error' ) &&
13261337 StringPrototypeStartsWith ( stack , name ) &&
@@ -1353,8 +1364,8 @@ function removeDuplicateErrorKeys(ctx, keys, err, stack) {
13531364 if ( ! ctx . showHidden && keys . length !== 0 ) {
13541365 for ( const name of [ 'name' , 'message' , 'stack' ] ) {
13551366 const index = ArrayPrototypeIndexOf ( keys , name ) ;
1356- // Only hide the property in case it's part of the original stack
1357- if ( index !== - 1 && StringPrototypeIncludes ( stack , err [ name ] ) ) {
1367+ // Only hide the property if it's a string and if it's part of the original stack
1368+ if ( index !== - 1 && ( typeof err [ name ] !== 'string' || StringPrototypeIncludes ( stack , err [ name ] ) ) ) {
13581369 ArrayPrototypeSplice ( keys , index , 1 ) ;
13591370 }
13601371 }
@@ -1414,7 +1425,7 @@ function safeGetCWD() {
14141425}
14151426
14161427function formatError ( err , constructor , tag , ctx , keys ) {
1417- const name = err . name != null ? String ( err . name ) : 'Error' ;
1428+ const name = err . name != null ? err . name : 'Error' ;
14181429 let stack = getStackString ( err ) ;
14191430
14201431 removeDuplicateErrorKeys ( ctx , keys , err , stack ) ;
0 commit comments