Skip to content

[release/8.0] [Blazor] Prevent error boundaries from handling exceptions of type NavigationException #53968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Components/Components/src/RenderTree/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,15 @@ private void HandleExceptionViaErrorBoundary(Exception error, ComponentState? er
// already on the sync context (and if not, we have a bug we want to know about).
Dispatcher.AssertAccess();

// We don't allow NavigationException instances to be caught by error boundaries.
// These are special exceptions whose purpose is to be as invisible as possible to
// user code and bubble all the way up to get handled by the framework as a redirect.
if (error is NavigationException)
{
HandleException(error);
return;
}

// Find the closest error boundary, if any
var candidate = errorSourceOrNull;
while (candidate is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ public void RedirectEnhancedNonBlazorPostToInternal()
// response to something like a 200 that the 'fetch' is allowed to read (embedding the
// destination URL).

[Fact]
public void RedirectEnhancedGetToInternalWithErrorBoundary()
{
// This test verifies that redirection works even if an ErrorBoundary wraps
// a component throwing a NavigationException.

Browser.Exists(By.LinkText("Enhanced GET with redirect inside error boundary")).Click();
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
Assert.EndsWith("/subdir/nav/scroll-to-hash?foo=%F0%9F%99%82", Browser.Url);

// See that 'back' takes you to the place from before the redirection
Browser.Navigate().Back();
Browser.Equal("Redirections", () => _originalH1Element.Text);
Assert.EndsWith("/subdir/redirect", Browser.Url);
}

private void AssertElementRemoved(IWebElement element)
{
Browser.True(() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@page "/redirect/error-boundary/get"

<h1>Redirect in error boundary GET</h1>

<ErrorBoundary>
<ChildContent>
<RedirectGet />
</ChildContent>
<ErrorContent>
<p>
Error caught by error boundary: @context.Message
</p>
</ErrorContent>
</ErrorBoundary>
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@
</form>
</li>
</ul>

<h2>Redirect inside error boundary</h2>
<ul>
<li><a href="redirect/error-boundary/get">Enhanced GET with redirect inside error boundary</a></li>
</ul>