@@ -643,25 +643,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
643643 return ctx . stylize ( base , 'date' ) ;
644644 }
645645 } else if ( isError ( value ) ) {
646- // Make error with message first say the error.
647- base = formatError ( value ) ;
648- // Wrap the error in brackets in case it has no stack trace.
649- const stackStart = base . indexOf ( '\n at' ) ;
650- if ( stackStart === - 1 ) {
651- base = `[${ base } ]` ;
652- }
653- // The message and the stack have to be indented as well!
654- if ( ctx . indentationLvl !== 0 ) {
655- const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
656- base = formatError ( value ) . replace ( / \n / g, `\n${ indentation } ` ) ;
657- }
646+ base = formatError ( value , constructor , tag , ctx ) ;
658647 if ( keys . length === 0 )
659648 return base ;
660-
661- if ( ctx . compact === false && stackStart !== - 1 ) {
662- braces [ 0 ] += `${ base . slice ( stackStart ) } ` ;
663- base = `[${ base . slice ( 0 , stackStart ) } ]` ;
664- }
665649 } else if ( isAnyArrayBuffer ( value ) ) {
666650 // Fast path for ArrayBuffer and SharedArrayBuffer.
667651 // Can't do the same for DataView because it has a non-primitive
@@ -821,6 +805,52 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
821805 return res ;
822806}
823807
808+ function formatError ( err , constructor , tag , ctx ) {
809+ // TODO(BridgeAR): Always show the error code if present.
810+ let stack = err . stack || errorToString ( err ) ;
811+
812+ // A stack trace may contain arbitrary data. Only manipulate the output
813+ // for "regular errors" (errors that "look normal") for now.
814+ const name = err . name || 'Error' ;
815+ let len = name . length ;
816+ if ( constructor === null ||
817+ name . endsWith ( 'Error' ) &&
818+ stack . startsWith ( name ) &&
819+ ( stack . length === len || stack [ len ] === ':' || stack [ len ] === '\n' ) ) {
820+ let fallback = 'Error' ;
821+ if ( constructor === null ) {
822+ const start = stack . match ( / ^ ( [ A - Z ] [ a - z _ A - Z 0 - 9 [ \] ( ) - ] + ) (?: : | \n { 4 } a t ) / ) ||
823+ stack . match ( / ^ ( [ a - z _ A - Z 0 - 9 - ] * E r r o r ) $ / ) ;
824+ fallback = start && start [ 1 ] || '' ;
825+ len = fallback . length ;
826+ fallback = fallback || 'Error' ;
827+ }
828+ const prefix = getPrefix ( constructor , tag , fallback ) . slice ( 0 , - 1 ) ;
829+ if ( name !== prefix ) {
830+ if ( prefix . includes ( name ) ) {
831+ if ( len === 0 ) {
832+ stack = `${ prefix } : ${ stack } ` ;
833+ } else {
834+ stack = `${ prefix } ${ stack . slice ( len ) } ` ;
835+ }
836+ } else {
837+ stack = `${ prefix } [${ name } ]${ stack . slice ( len ) } ` ;
838+ }
839+ }
840+ }
841+ // Wrap the error in brackets in case it has no stack trace.
842+ const stackStart = stack . indexOf ( '\n at' ) ;
843+ if ( stackStart === - 1 ) {
844+ stack = `[${ stack } ]` ;
845+ }
846+ // The message and the stack have to be indented as well!
847+ if ( ctx . indentationLvl !== 0 ) {
848+ const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
849+ stack = stack . replace ( / \n / g, `\n${ indentation } ` ) ;
850+ }
851+ return stack ;
852+ }
853+
824854function groupArrayElements ( ctx , output ) {
825855 let totalLength = 0 ;
826856 let maxLength = 0 ;
@@ -968,10 +998,6 @@ function formatPrimitive(fn, value, ctx) {
968998 return fn ( value . toString ( ) , 'symbol' ) ;
969999}
9701000
971- function formatError ( value ) {
972- return value . stack || errorToString ( value ) ;
973- }
974-
9751001function formatNamespaceObject ( ctx , value , recurseTimes , keys ) {
9761002 const output = new Array ( keys . length ) ;
9771003 for ( var i = 0 ; i < keys . length ; i ++ ) {
0 commit comments