Skip to content

Commit 6f5cf6e

Browse files
authored
Implement PageError (#186)
1 parent cec306f commit 6f5cf6e

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

lib/PuppeteerSharp.Tests/Page/Events/ErrorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ public async Task ShouldThrowWhenPageCrashes()
2020
Assert.Equal("Page crashed!", error);
2121
}
2222
}
23-
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+

2+
using System;
3+
using System.Linq;
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace PuppeteerSharp.Tests.Page.Events
9+
{
10+
[Collection("PuppeteerLoaderFixture collection")]
11+
public class PageErrorTests : PuppeteerPageBaseTest
12+
{
13+
[Fact]
14+
public async Task ShouldFire()
15+
{
16+
string error = null;
17+
void EventHandler(object sender, PageErrorEventArgs e)
18+
{
19+
error = e.Message;
20+
Page.PageError -= EventHandler;
21+
}
22+
23+
Page.PageError += EventHandler;
24+
25+
await Task.WhenAll(
26+
Page.GoToAsync(TestConstants.ServerUrl + "/error.html"),
27+
WaitForEvents(Page.Client, "Runtime.exceptionThrown")
28+
);
29+
30+
Assert.Contains("Fancy", error);
31+
}
32+
}
33+
}

lib/PuppeteerSharp/Page.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace PuppeteerSharp
4+
{
5+
public class PageErrorEventArgs : EventArgs
6+
{
7+
public string Message { get; set; }
8+
9+
public PageErrorEventArgs(string message) => Message = message;
10+
}
11+
}

0 commit comments

Comments
 (0)