Skip to content

Commit

Permalink
Capture navigation exceptions on shell controlled navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
albyrock87 committed Mar 21, 2024
1 parent 481cbf2 commit a86b391
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Source/Nalu.Maui.Navigation/Internals/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<bool> GoToAsync(INavigationInfo navigation)
{
if (navigation.Count == 0)
{
throw new InvalidOperationException("Navigation must contain at least one segment.");
throw new InvalidNavigationException("Navigation must contain at least one segment.");
}

var disposeBag = new HashSet<object>();
Expand Down Expand Up @@ -165,7 +165,7 @@ private async Task<bool> ExecuteRelativeNavigationAsync(
var popCount = navigation.Count(segment => segment.SegmentName == NavigationPop.PopRoute);
if (popCount >= stack.Count)
{
throw new InvalidOperationException("Cannot pop more pages than the stack contains.");
throw new InvalidNavigationException("Cannot pop more pages than the stack contains.");
}

var navigationCount = navigation.Count;
Expand Down Expand Up @@ -465,7 +465,7 @@ private async Task<bool> ExecuteNavigationAsync(Func<Task<bool>> navigationFunc)
var taken = await _semaphore.WaitAsync(0).ConfigureAwait(true);
if (!taken)
{
throw new InvalidOperationException("Cannot navigate while another navigation is in progress, try to use IDispatcher.DispatchDelayed.");
throw new InvalidNavigationException("Cannot navigate while another navigation is in progress, try to use IDispatcher.DispatchDelayed.");
}

try
Expand Down Expand Up @@ -517,7 +517,7 @@ private void DisposePage(object toDispose)

default:
{
throw new InvalidOperationException();
throw new InvalidNavigationException("Trying to dispose an unknown object.");
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions Source/Nalu.Maui.Navigation/InvalidNavigationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Nalu;

/// <summary>
/// Represents an exception thrown when triggering a navigation that cannot be performed.
/// </summary>
public class InvalidNavigationException(string? message = null, Exception? innerException = null)
: Exception(message, innerException);
13 changes: 12 additions & 1 deletion Source/Nalu.Maui.Navigation/NaluShell.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Nalu;

using System.Diagnostics;
using System.Text.RegularExpressions;

#pragma warning disable IDE0290
Expand Down Expand Up @@ -150,7 +151,17 @@ protected override async void OnNavigating(ShellNavigatingEventArgs args)

await Task.Yield();
await Task.Yield();
await _navigationService.GoToAsync(navigation).ConfigureAwait(true);
try
{
await _navigationService.GoToAsync(navigation).ConfigureAwait(true);
}
catch (InvalidNavigationException ex)
{
if (Debugger.IsAttached)
{
Console.WriteLine(ex.Message);
}
}
}
}

Expand Down

0 comments on commit a86b391

Please sign in to comment.