File tree Expand file tree Collapse file tree 6 files changed +40
-16
lines changed Expand file tree Collapse file tree 6 files changed +40
-16
lines changed Original file line number Diff line number Diff line change @@ -91,9 +91,21 @@ export function toConsoleMessageLocation(stackTrace: Protocol.Runtime.StackTrace
91
91
}
92
92
93
93
export function exceptionToError ( exceptionDetails : Protocol . Runtime . ExceptionDetails ) : Error {
94
- const message = getExceptionMessage ( exceptionDetails ) ;
94
+ const messageWithStack = getExceptionMessage ( exceptionDetails ) ;
95
+ const lines = messageWithStack . split ( '\n' ) ;
96
+ const firstStackTraceLine = lines . findIndex ( line => line . startsWith ( ' at' ) ) ;
97
+ let message = '' ;
98
+ let stack = '' ;
99
+ if ( firstStackTraceLine === - 1 ) {
100
+ message = messageWithStack ;
101
+ } else {
102
+ message = lines . slice ( 0 , firstStackTraceLine ) . join ( '\n' ) ;
103
+ stack = messageWithStack ;
104
+ }
105
+ const match = message . match ( / ^ [ a - z A - Z 0 - 0 _ ] * E r r o r : ( .* ) $ / ) ;
106
+ if ( match )
107
+ message = match [ 1 ] ;
95
108
const err = new Error ( message ) ;
96
- // Don't report clientside error with a node stack attached
97
- err . stack = 'Error: ' + err . message ; // Stack is supposed to contain error message as the first line.
109
+ err . stack = stack ;
98
110
return err ;
99
111
}
Original file line number Diff line number Diff line change @@ -184,7 +184,8 @@ export class FFPage implements PageDelegate {
184
184
}
185
185
186
186
_onUncaughtError ( params : Protocol . Page . uncaughtErrorPayload ) {
187
- const error = new Error ( params . message ) ;
187
+ const message = params . message . startsWith ( 'Error: ' ) ? params . message . substring ( 7 ) : params . message ;
188
+ const error = new Error ( message ) ;
188
189
error . stack = params . stack ;
189
190
this . _page . emit ( Events . Page . PageError , error ) ;
190
191
}
Original file line number Diff line number Diff line change @@ -149,16 +149,13 @@ class Helper {
149
149
Helper . removeEventListeners ( [ listener ] ) ;
150
150
clearTimeout ( eventTimeout ) ;
151
151
}
152
- const result = await Promise . race ( [ promise , abortPromise ] ) . then ( r => {
152
+ return await Promise . race ( [ promise , abortPromise ] ) . then ( r => {
153
153
cleanup ( ) ;
154
154
return r ;
155
155
} , e => {
156
156
cleanup ( ) ;
157
157
throw e ;
158
158
} ) ;
159
- if ( result instanceof Error )
160
- throw result ;
161
- return result ;
162
159
}
163
160
164
161
static async waitWithTimeout < T > ( promise : Promise < T > , taskName : string , timeout : number ) : Promise < T > {
Original file line number Diff line number Diff line change @@ -454,8 +454,15 @@ export class WKPage implements PageDelegate {
454
454
return ;
455
455
}
456
456
if ( level === 'error' && source === 'javascript' ) {
457
- const error = new Error ( text ) ;
458
- error . stack = 'Error: ' + error . message ; // Nullify stack. Stack is supposed to contain error message as the first line.
457
+ const message = text . startsWith ( 'Error: ' ) ? text . substring ( 7 ) : text ;
458
+ const error = new Error ( message ) ;
459
+ if ( event . message . stackTrace ) {
460
+ error . stack = event . message . stackTrace . map ( callFrame => {
461
+ return `${ callFrame . functionName } @${ callFrame . url } :${ callFrame . lineNumber } :${ callFrame . columnNumber } ` ;
462
+ } ) . join ( '\n' ) ;
463
+ } else {
464
+ error . stack = '' ;
465
+ }
459
466
this . _page . emit ( Events . Page . PageError , error ) ;
460
467
return ;
461
468
}
Original file line number Diff line number Diff line change 11
11
}
12
12
13
13
function c ( ) {
14
- throw new Error ( 'Fancy error!' ) ;
14
+ window . e = new Error ( 'Fancy error!' ) ;
15
+ throw window . e ;
15
16
}
17
+ //# sourceURL=myscript.js
16
18
</ script >
Original file line number Diff line number Diff line change @@ -493,13 +493,18 @@ describe('Page.exposeFunction', function() {
493
493
494
494
describe ( 'Page.Events.PageError' , function ( ) {
495
495
it ( 'should fire' , async ( { page, server} ) => {
496
- let error = null ;
497
- page . once ( 'pageerror' , e => error = e ) ;
498
- await Promise . all ( [
496
+ const [ error ] = await Promise . all ( [
497
+ page . waitForEvent ( 'pageerror' ) ,
499
498
page . goto ( server . PREFIX + '/error.html' ) ,
500
- new Promise ( f => page . on ( 'pageerror' , f ) )
501
499
] ) ;
502
- expect ( error . message ) . toContain ( 'Fancy' ) ;
500
+ expect ( error . name ) . toBe ( 'Error' ) ;
501
+ expect ( error . message ) . toBe ( 'Fancy error!' ) ;
502
+ let stack = await page . evaluate ( ( ) => window . e . stack ) ;
503
+ // Note that WebKit does not use sourceURL for some reason and reports the stack of the 'throw' statement
504
+ // instead of the Error constructor call.
505
+ if ( WEBKIT )
506
+ stack = stack . replace ( '14:25' , '15:19' ) ;
507
+ expect ( error . stack ) . toBe ( stack ) ;
503
508
} ) ;
504
509
} ) ;
505
510
You can’t perform that action at this time.
0 commit comments