Skip to content

Wrap OnNavigateTo callback with GetErrorHandledTask for proper exception logging #62414

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
92 changes: 92 additions & 0 deletions src/Components/Components/test/NavigationManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,38 @@ public void OnNotFoundSubscriptionIsTriggeredWhenNotFoundCalled()
// Assert
Assert.True(notFoundTriggered, "The OnNotFound event was not triggered as expected.");
}

[Fact]
public void OnNavigateToCallback_WhenThrows_ShouldBeHandledGracefully()
{
// Arrange
var baseUri = "scheme://host/";
var uri = "scheme://host/test";
var testNavManager = new TestNavigationManagerWithCallback();
var exceptionThrown = false;
var expectedException = new InvalidOperationException("Test exception from OnNavigateTo");

// Configure the onNavigateTo callback to throw an exception
testNavManager.ConfigureOnNavigateToCallback(throwingUri =>
{
exceptionThrown = true;
throw expectedException;
});

// Act
// Initialize the navigation manager with the callback
testNavManager.Initialize(baseUri, uri, testNavManager.GetOnNavigateToCallback());

// Assert
Assert.True(testNavManager.IsInitialized);

// When navigation is triggered, the exception should be handled gracefully
var thrownException = testNavManager.TriggerOnNavigateToCallback(uri);

// Assert
Assert.True(exceptionThrown, "The OnNavigateTo callback should have been called and thrown an exception.");
Assert.Same(expectedException, thrownException);
}

private class TestNavigationManager : NavigationManager
{
Expand Down Expand Up @@ -932,4 +964,64 @@ protected override void HandleLocationChangingHandlerException(Exception ex, Loc
_exceptionsThrownFromLocationChangingHandlers.Add(ex);
}
}

private class TestNavigationManagerWithCallback : TestNavigationManager, IHostEnvironmentNavigationManager
{
private Func<string, Task> _onNavigateToCallback;

public TestNavigationManagerWithCallback()
{
}

public void Initialize(string baseUri, string uri, Func<string, Task> onNavigateTo)
{
_onNavigateToCallback = onNavigateTo;
base.Initialize(baseUri, uri);
}

public void ConfigureOnNavigateToCallback(Func<string, Task> callback)
{
_onNavigateToCallback = callback;
}

public Func<string, Task> GetOnNavigateToCallback()
{
return _onNavigateToCallback;
}

public Exception TriggerOnNavigateToCallback(string uri)
{
if (_onNavigateToCallback == null)
{
return null;
}

try
{
// Simulate the fire-and-forget pattern used in RemoteNavigationManager
_ = _onNavigateToCallback(uri);
return null;
}
catch (Exception ex)
{
return ex;
}
}

public bool IsInitialized
{
get
{
try
{
_ = BaseUri; // This will throw if not initialized
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ internal async Task InitializeStandardComponentServicesAsync(
IFormCollection? form = null)
{
var navigationManager = httpContext.RequestServices.GetRequiredService<NavigationManager>();
((IHostEnvironmentNavigationManager)navigationManager)?.Initialize(GetContextBaseUri(httpContext.Request), GetFullUri(httpContext.Request), OnNavigateTo);
((IHostEnvironmentNavigationManager)navigationManager)?.Initialize(
GetContextBaseUri(httpContext.Request),
GetFullUri(httpContext.Request),
uri => GetErrorHandledTask(OnNavigateTo(uri)));

navigationManager?.OnNotFound += (sender, args) =>
{
Expand Down
Loading