66
77import {
88 createStackTraceForConsoleMessage ,
9+ createStackTrace ,
910 type TargetUniverse ,
1011} from '../DevtoolsUtils.js' ;
12+ import type { UncaughtError } from '../PageCollector.js' ;
1113import type * as DevTools from '../third_party/index.js' ;
1214import type { ConsoleMessage } from '../third_party/index.js' ;
1315
@@ -19,13 +21,13 @@ export interface ConsoleFormatterOptions {
1921}
2022
2123export class ConsoleFormatter {
22- #msg: ConsoleMessage | Error ;
24+ #msg: ConsoleMessage | Error | UncaughtError ;
2325 #resolvedArgs: unknown [ ] = [ ] ;
2426 #resolvedStackTrace?: DevTools . DevTools . StackTrace . StackTrace . StackTrace ;
2527 #id?: number ;
2628
2729 private constructor (
28- msg : ConsoleMessage | Error ,
30+ msg : ConsoleMessage | Error | UncaughtError ,
2931 options ?: ConsoleFormatterOptions ,
3032 ) {
3133 this . #msg = msg ;
@@ -34,7 +36,7 @@ export class ConsoleFormatter {
3436 }
3537
3638 static async from (
37- msg : ConsoleMessage | Error ,
39+ msg : ConsoleMessage | Error | UncaughtError ,
3840 options ?: ConsoleFormatterOptions ,
3941 ) : Promise < ConsoleFormatter > {
4042 const formatter = new ConsoleFormatter ( msg , options ) ;
@@ -44,27 +46,44 @@ export class ConsoleFormatter {
4446 return formatter ;
4547 }
4648
49+ #isConsoleMessage(
50+ msg : ConsoleMessage | Error | UncaughtError ,
51+ ) : msg is ConsoleMessage {
52+ // No `instanceof` as tests mock `ConsoleMessage`.
53+ return 'args' in msg && typeof msg . args === 'function' ;
54+ }
55+
4756 async #loadDetailedData( devTools ?: TargetUniverse ) : Promise < void > {
4857 if ( this . #msg instanceof Error ) {
4958 return ;
5059 }
5160
52- this . #resolvedArgs = await Promise . all (
53- this . #msg. args ( ) . map ( async ( arg , i ) => {
54- try {
55- return await arg . jsonValue ( ) ;
56- } catch {
57- return `<error: Argument ${ i } is no longer available>` ;
58- }
59- } ) ,
60- ) ;
61+ if ( this . #isConsoleMessage( this . #msg) ) {
62+ this . #resolvedArgs = await Promise . all (
63+ this . #msg. args ( ) . map ( async ( arg , i ) => {
64+ try {
65+ return await arg . jsonValue ( ) ;
66+ } catch {
67+ return `<error: Argument ${ i } is no longer available>` ;
68+ }
69+ } ) ,
70+ ) ;
71+ }
6172
6273 if ( devTools ) {
6374 try {
64- this . #resolvedStackTrace = await createStackTraceForConsoleMessage (
65- devTools ,
66- this . #msg,
67- ) ;
75+ if ( this . #isConsoleMessage( this . #msg) ) {
76+ this . #resolvedStackTrace = await createStackTraceForConsoleMessage (
77+ devTools ,
78+ this . #msg,
79+ ) ;
80+ } else if ( this . #msg. stackTrace ) {
81+ this . #resolvedStackTrace = await createStackTrace (
82+ devTools ,
83+ this . #msg. stackTrace ,
84+ this . #msg. targetId ,
85+ ) ;
86+ }
6887 } catch {
6988 // ignore
7089 }
@@ -75,10 +94,7 @@ export class ConsoleFormatter {
7594 toString ( ) : string {
7695 const type = this . #getType( ) ;
7796 const text = this . #getText( ) ;
78- const argsCount =
79- this . #msg instanceof Error
80- ? 0
81- : this . #resolvedArgs. length || this . #msg. args ( ) . length ;
97+ const argsCount = this . #getArgsCount( ) ;
8298 const idPart = this . #id !== undefined ? `msgid=${ this . #id} ` : '' ;
8399 return `${ idPart } [${ type } ] ${ text } (${ argsCount } args)` ;
84100 }
@@ -95,21 +111,21 @@ export class ConsoleFormatter {
95111 }
96112
97113 #getType( ) : string {
98- if ( this . #msg instanceof Error ) {
114+ if ( ! this . #isConsoleMessage ( this . # msg) ) {
99115 return 'error' ;
100116 }
101117 return this . #msg. type ( ) ;
102118 }
103119
104120 #getText( ) : string {
105- if ( this . #msg instanceof Error ) {
121+ if ( ! this . #isConsoleMessage ( this . # msg) ) {
106122 return this . #msg. message ;
107123 }
108124 return this . #msg. text ( ) ;
109125 }
110126
111127 #getArgs( ) : unknown [ ] {
112- if ( this . #msg instanceof Error ) {
128+ if ( ! this . #isConsoleMessage ( this . # msg) ) {
113129 return [ ] ;
114130 }
115131 if ( this . #resolvedArgs. length > 0 ) {
@@ -123,6 +139,13 @@ export class ConsoleFormatter {
123139 return [ ] ;
124140 }
125141
142+ #getArgsCount( ) : number {
143+ if ( ! this . #isConsoleMessage( this . #msg) ) {
144+ return 0 ;
145+ }
146+ return this . #resolvedArgs. length || this . #msg. args ( ) . length ;
147+ }
148+
126149 #formatArg( arg : unknown ) {
127150 return typeof arg === 'object' ? JSON . stringify ( arg ) : String ( arg ) ;
128151 }
@@ -185,10 +208,7 @@ export class ConsoleFormatter {
185208 return {
186209 type : this . #getType( ) ,
187210 text : this . #getText( ) ,
188- argsCount :
189- this . #msg instanceof Error
190- ? 0
191- : this . #resolvedArgs. length || this . #msg. args ( ) . length ,
211+ argsCount : this . #getArgsCount( ) ,
192212 id : this . #id,
193213 } ;
194214 }
0 commit comments