@@ -91,6 +91,10 @@ private Page(Session client, Target target, FrameTree frameTree, bool ignoreHTTP
9191 public event EventHandler < RequestEventArgs > RequestCreated ;
9292 public event EventHandler < RequestEventArgs > RequestFinished ;
9393 public event EventHandler < RequestEventArgs > RequestFailed ;
94+ /// <summary>
95+ /// Emitted when an uncaught exception happens within the page.
96+ /// </summary>
97+ public event EventHandler < PageErrorEventArgs > PageError ;
9498
9599 internal Session Client { get ; }
96100
@@ -172,7 +176,7 @@ public async Task<JSHandle> EvaluateExpressionHandleAsync(string script)
172176 /// <summary>
173177 /// Executes a script in browser context
174178 /// </summary>
175- /// <param name="script ">Script to be evaluated in browser context</param>
179+ /// <param name="pageFunction ">Script to be evaluated in browser context</param>
176180 /// <remarks>
177181 /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.
178182 /// <see cref="JSHandle"/> instances can be passed as arguments
@@ -538,7 +542,7 @@ public Task CloseAsync()
538542
539543 return Task . CompletedTask ;
540544 }
541-
545+
542546 public Task < dynamic > EvaluateExpressionAsync ( string script )
543547 => _frameManager . MainFrame . EvaluateExpressionAsync ( script ) ;
544548
@@ -849,7 +853,25 @@ private async Task OnCertificateError(MessageEventArgs e)
849853 }
850854
851855 private void HandleException ( dynamic exceptionDetails )
856+ => PageError ? . Invoke ( this , new PageErrorEventArgs ( GetExceptionMessage ( exceptionDetails ) ) ) ;
857+
858+ private string GetExceptionMessage ( dynamic exceptionDetails )
852859 {
860+ if ( exceptionDetails . exception != null )
861+ {
862+ return exceptionDetails . exception . description ;
863+ }
864+ var message = exceptionDetails . text ;
865+ if ( exceptionDetails . stackTrace )
866+ {
867+ foreach ( var callframe in exceptionDetails . stackTrace . callFrames )
868+ {
869+ var location = $ "{ callframe . url } :{ callframe . lineNumber } :{ callframe . columnNumber } ";
870+ var functionName = callframe . functionName || "<anonymous>" ;
871+ message += $ "\n at { functionName } ({ location } )";
872+ }
873+ }
874+ return message ;
853875 }
854876
855877 private void OnDialog ( PageJavascriptDialogOpeningResponse message )
0 commit comments