@@ -666,25 +666,9 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
666
666
return ctx . stylize ( base , 'date' ) ;
667
667
}
668
668
} else if ( isError ( value ) ) {
669
- // Make error with message first say the error.
670
- base = formatError ( value ) ;
671
- // Wrap the error in brackets in case it has no stack trace.
672
- const stackStart = base . indexOf ( '\n at' ) ;
673
- if ( stackStart === - 1 ) {
674
- base = `[${ base } ]` ;
675
- }
676
- // The message and the stack have to be indented as well!
677
- if ( ctx . indentationLvl !== 0 ) {
678
- const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
679
- base = formatError ( value ) . replace ( / \n / g, `\n${ indentation } ` ) ;
680
- }
669
+ base = formatError ( value , constructor , tag , ctx ) ;
681
670
if ( keys . length === 0 )
682
671
return base ;
683
-
684
- if ( ctx . compact === false && stackStart !== - 1 ) {
685
- braces [ 0 ] += `${ base . slice ( stackStart ) } ` ;
686
- base = `[${ base . slice ( 0 , stackStart ) } ]` ;
687
- }
688
672
} else if ( isAnyArrayBuffer ( value ) ) {
689
673
// Fast path for ArrayBuffer and SharedArrayBuffer.
690
674
// Can't do the same for DataView because it has a non-primitive
@@ -844,6 +828,52 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
844
828
return res ;
845
829
}
846
830
831
+ function formatError ( err , constructor , tag , ctx ) {
832
+ // TODO(BridgeAR): Always show the error code if present.
833
+ let stack = err . stack || errorToString ( err ) ;
834
+
835
+ // A stack trace may contain arbitrary data. Only manipulate the output
836
+ // for "regular errors" (errors that "look normal") for now.
837
+ const name = err . name || 'Error' ;
838
+ let len = name . length ;
839
+ if ( constructor === null ||
840
+ name . endsWith ( 'Error' ) &&
841
+ stack . startsWith ( name ) &&
842
+ ( stack . length === len || stack [ len ] === ':' || stack [ len ] === '\n' ) ) {
843
+ let fallback = 'Error' ;
844
+ if ( constructor === null ) {
845
+ const start = stack . match ( / ^ ( [ A - Z ] [ a - z _ A - Z 0 - 9 [ \] ( ) - ] + ) (?: : | \n { 4 } a t ) / ) ||
846
+ stack . match ( / ^ ( [ a - z _ A - Z 0 - 9 - ] * E r r o r ) $ / ) ;
847
+ fallback = start && start [ 1 ] || '' ;
848
+ len = fallback . length ;
849
+ fallback = fallback || 'Error' ;
850
+ }
851
+ const prefix = getPrefix ( constructor , tag , fallback ) . slice ( 0 , - 1 ) ;
852
+ if ( name !== prefix ) {
853
+ if ( prefix . includes ( name ) ) {
854
+ if ( len === 0 ) {
855
+ stack = `${ prefix } : ${ stack } ` ;
856
+ } else {
857
+ stack = `${ prefix } ${ stack . slice ( len ) } ` ;
858
+ }
859
+ } else {
860
+ stack = `${ prefix } [${ name } ]${ stack . slice ( len ) } ` ;
861
+ }
862
+ }
863
+ }
864
+ // Wrap the error in brackets in case it has no stack trace.
865
+ const stackStart = stack . indexOf ( '\n at' ) ;
866
+ if ( stackStart === - 1 ) {
867
+ stack = `[${ stack } ]` ;
868
+ }
869
+ // The message and the stack have to be indented as well!
870
+ if ( ctx . indentationLvl !== 0 ) {
871
+ const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
872
+ stack = stack . replace ( / \n / g, `\n${ indentation } ` ) ;
873
+ }
874
+ return stack ;
875
+ }
876
+
847
877
function groupArrayElements ( ctx , output ) {
848
878
let totalLength = 0 ;
849
879
let maxLength = 0 ;
@@ -991,11 +1021,6 @@ function formatPrimitive(fn, value, ctx) {
991
1021
return fn ( value . toString ( ) , 'symbol' ) ;
992
1022
}
993
1023
994
- function formatError ( value ) {
995
- // TODO(BridgeAR): Always show the error code if present.
996
- return value . stack || errorToString ( value ) ;
997
- }
998
-
999
1024
function formatNamespaceObject ( ctx , value , recurseTimes , keys ) {
1000
1025
const output = new Array ( keys . length ) ;
1001
1026
for ( var i = 0 ; i < keys . length ; i ++ ) {
0 commit comments