Skip to content

Commit 332796a

Browse files
kblokMeir017
authored andcommitted
Introduce url at error message at page.goto (#385)
* Introduce url at error message at page.goto * Code Review
1 parent ccf6e01 commit 332796a

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

lib/PuppeteerSharp.Tests/PageTests/GotoTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ public async Task ShouldWorkWithSelfRequestingPage()
263263
Assert.Contains("self-request.html", response.Url);
264264
}
265265

266+
[Fact]
267+
public async Task ShouldFailWhenNavigatingAndShowTheUrlAtTheErrorMessage()
268+
{
269+
var url = TestConstants.HttpsPrefix + "/redirect/1.html";
270+
var exception = await Assert.ThrowsAnyAsync<NavigationException>(async () => await Page.GoToAsync(url));
271+
Assert.Contains(url, exception.Message);
272+
Assert.Contains(url, exception.Url);
273+
}
274+
266275
[Fact]
267276
public async Task ResponseOkShouldBeTrueForFile()
268277
{

lib/PuppeteerSharp/NavigationException.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ namespace PuppeteerSharp
99
[Serializable]
1010
public class NavigationException : PuppeteerException
1111
{
12+
/// <summary>
13+
/// Url that caused the exception
14+
/// </summary>
15+
/// <value>The URL.</value>
16+
public string Url { get; }
17+
1218
/// <summary>
1319
/// Initializes a new instance of the <see cref="NavigationException"/> class.
1420
/// </summary>
@@ -28,11 +34,20 @@ public NavigationException(string message) : base(message)
2834
/// Initializes a new instance of the <see cref="NavigationException"/> class.
2935
/// </summary>
3036
/// <param name="message">Message.</param>
31-
/// <param name="innerException">Inner exception.</param>
32-
public NavigationException(string message, Exception innerException) : base(message, innerException)
37+
/// <param name="url">Url.</param>
38+
public NavigationException(string message, string url) : base(message)
3339
{
40+
Url = url;
3441
}
3542

43+
/// <summary>
44+
/// Initializes a new instance of the <see cref="NavigationException"/> class.
45+
/// </summary>
46+
/// <param name="message">Message.</param>
47+
/// <param name="innerException">Inner exception.</param>
48+
public NavigationException(string message, Exception innerException) : base(message, innerException)
49+
=> Url = (innerException as NavigationException)?.Url;
50+
3651
/// <summary>
3752
/// Initializes a new instance of the <see cref="NavigationException"/> class.
3853
/// </summary>
@@ -41,5 +56,19 @@ public NavigationException(string message, Exception innerException) : base(mess
4156
protected NavigationException(SerializationInfo info, StreamingContext context) : base(info, context)
4257
{
4358
}
59+
60+
/// <inheritdoc/>
61+
public override string Message
62+
{
63+
get
64+
{
65+
if (string.IsNullOrEmpty(Url))
66+
{
67+
return base.Message;
68+
}
69+
70+
return $"{base.Message} at {Url}";
71+
}
72+
}
4473
}
4574
}

lib/PuppeteerSharp/Page.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,13 @@ public async Task<Response> GoToAsync(string url, NavigationOptions options)
649649
var referrer = _networkManager.ExtraHTTPHeaders?.GetValueOrDefault("referer");
650650
var requests = new Dictionary<string, Request>();
651651

652-
EventHandler<RequestEventArgs> createRequestEventListener = (object sender, RequestEventArgs e) =>
652+
void createRequestEventListener(object sender, RequestEventArgs e)
653653
{
654654
if (!requests.ContainsKey(e.Request.Url))
655655
{
656656
requests.Add(e.Request.Url, e.Request);
657657
}
658-
};
658+
}
659659

660660
_networkManager.Request += createRequestEventListener;
661661

@@ -1283,7 +1283,7 @@ public async Task<Response> WaitForNavigationAsync(NavigationOptions options = n
12831283
var watcher = new NavigatorWatcher(_frameManager, mainFrame, timeout, options);
12841284
var responses = new Dictionary<string, Response>();
12851285

1286-
EventHandler<ResponseCreatedEventArgs> createResponseEventListener = (sender, e) => responses[e.Response.Url] = e.Response;
1286+
void createResponseEventListener(object sender, ResponseCreatedEventArgs e) => responses[e.Response.Url] = e.Response;
12871287

12881288
_networkManager.Response += createResponseEventListener;
12891289

@@ -1306,15 +1306,15 @@ public async Task<Response> WaitForNavigationAsync(NavigationOptions options = n
13061306
/// <returns>Task which which resolves to the main resource response. In case of multiple redirects,
13071307
/// the navigation will resolve with the response of the last redirect. If can not go back, resolves to null.</returns>
13081308
/// <param name="options">Navigation parameters.</param>
1309-
public Task<Response> GoBackAsync(NavigationOptions options = null) => GoAsync(-1, null);
1309+
public Task<Response> GoBackAsync(NavigationOptions options = null) => GoAsync(-1, options);
13101310

13111311
/// <summary>
13121312
/// Navigate to the next page in history.
13131313
/// </summary>
13141314
/// <returns>Task which which resolves to the main resource response. In case of multiple redirects,
13151315
/// the navigation will resolve with the response of the last redirect. If can not go forward, resolves to null.</returns>
13161316
/// <param name="options">Navigation parameters.</param>
1317-
public Task<Response> GoForwardAsync(NavigationOptions options = null) => GoAsync(1, null);
1317+
public Task<Response> GoForwardAsync(NavigationOptions options = null) => GoAsync(1, options);
13181318

13191319
#endregion
13201320

@@ -1402,7 +1402,7 @@ private async Task<byte[]> PerformScreenshot(string format, ScreenshotOptions op
14021402
targetId = Target.TargetId
14031403
});
14041404

1405-
var clip = options.Clip != null ? options.Clip.Clone() : null;
1405+
var clip = options.Clip?.Clone();
14061406
if (clip != null)
14071407
{
14081408
clip.Scale = 1;
@@ -1740,7 +1740,7 @@ private async Task Navigate(CDPSession client, string url, string referrer)
17401740

17411741
if (response.errorText != null)
17421742
{
1743-
throw new NavigationException(response.errorText.ToString());
1743+
throw new NavigationException(response.errorText.ToString(), url);
17441744
}
17451745
}
17461746

0 commit comments

Comments
 (0)