Skip to content

Commit f734954

Browse files
Hide NavigationException from error boundaries (#53826) (#53968)
1 parent 414f0d4 commit f734954

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/Components/Components/src/RenderTree/Renderer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,15 @@ private void HandleExceptionViaErrorBoundary(Exception error, ComponentState? er
10651065
// already on the sync context (and if not, we have a bug we want to know about).
10661066
Dispatcher.AssertAccess();
10671067

1068+
// We don't allow NavigationException instances to be caught by error boundaries.
1069+
// These are special exceptions whose purpose is to be as invisible as possible to
1070+
// user code and bubble all the way up to get handled by the framework as a redirect.
1071+
if (error is NavigationException)
1072+
{
1073+
HandleException(error);
1074+
return;
1075+
}
1076+
10681077
// Find the closest error boundary, if any
10691078
var candidate = errorSourceOrNull;
10701079
while (candidate is not null)

src/Components/test/E2ETest/ServerRenderingTests/RedirectionTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public void RedirectEnhancedNonBlazorPostToInternal()
205205
// response to something like a 200 that the 'fetch' is allowed to read (embedding the
206206
// destination URL).
207207

208+
[Fact]
209+
public void RedirectEnhancedGetToInternalWithErrorBoundary()
210+
{
211+
// This test verifies that redirection works even if an ErrorBoundary wraps
212+
// a component throwing a NavigationException.
213+
214+
Browser.Exists(By.LinkText("Enhanced GET with redirect inside error boundary")).Click();
215+
Browser.Equal("Scroll to hash", () => _originalH1Element.Text);
216+
Assert.EndsWith("/subdir/nav/scroll-to-hash?foo=%F0%9F%99%82", Browser.Url);
217+
218+
// See that 'back' takes you to the place from before the redirection
219+
Browser.Navigate().Back();
220+
Browser.Equal("Redirections", () => _originalH1Element.Text);
221+
Assert.EndsWith("/subdir/redirect", Browser.Url);
222+
}
223+
208224
private void AssertElementRemoved(IWebElement element)
209225
{
210226
Browser.True(() =>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@page "/redirect/error-boundary/get"
2+
3+
<h1>Redirect in error boundary GET</h1>
4+
5+
<ErrorBoundary>
6+
<ChildContent>
7+
<RedirectGet />
8+
</ChildContent>
9+
<ErrorContent>
10+
<p>
11+
Error caught by error boundary: @context.Message
12+
</p>
13+
</ErrorContent>
14+
</ErrorBoundary>

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/Redirections/RedirectHome.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@
9090
</form>
9191
</li>
9292
</ul>
93+
94+
<h2>Redirect inside error boundary</h2>
95+
<ul>
96+
<li><a href="redirect/error-boundary/get">Enhanced GET with redirect inside error boundary</a></li>
97+
</ul>

0 commit comments

Comments
 (0)