From a86b391591eee62cdd24b1cd7eda9398b1367697 Mon Sep 17 00:00:00 2001 From: Alberto Aldegheri Date: Thu, 21 Mar 2024 12:05:22 +0100 Subject: [PATCH] Capture navigation exceptions on shell controlled navigation --- .../Internals/NavigationService.cs | 8 ++++---- .../InvalidNavigationException.cs | 7 +++++++ Source/Nalu.Maui.Navigation/NaluShell.cs | 13 ++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 Source/Nalu.Maui.Navigation/InvalidNavigationException.cs diff --git a/Source/Nalu.Maui.Navigation/Internals/NavigationService.cs b/Source/Nalu.Maui.Navigation/Internals/NavigationService.cs index 346f47b..7d59fa6 100644 --- a/Source/Nalu.Maui.Navigation/Internals/NavigationService.cs +++ b/Source/Nalu.Maui.Navigation/Internals/NavigationService.cs @@ -55,7 +55,7 @@ public async Task 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(); @@ -165,7 +165,7 @@ private async Task 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; @@ -465,7 +465,7 @@ private async Task ExecuteNavigationAsync(Func> 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 @@ -517,7 +517,7 @@ private void DisposePage(object toDispose) default: { - throw new InvalidOperationException(); + throw new InvalidNavigationException("Trying to dispose an unknown object."); } } } diff --git a/Source/Nalu.Maui.Navigation/InvalidNavigationException.cs b/Source/Nalu.Maui.Navigation/InvalidNavigationException.cs new file mode 100644 index 0000000..5d570f5 --- /dev/null +++ b/Source/Nalu.Maui.Navigation/InvalidNavigationException.cs @@ -0,0 +1,7 @@ +namespace Nalu; + +/// +/// Represents an exception thrown when triggering a navigation that cannot be performed. +/// +public class InvalidNavigationException(string? message = null, Exception? innerException = null) + : Exception(message, innerException); diff --git a/Source/Nalu.Maui.Navigation/NaluShell.cs b/Source/Nalu.Maui.Navigation/NaluShell.cs index 1b5cae5..07054e3 100644 --- a/Source/Nalu.Maui.Navigation/NaluShell.cs +++ b/Source/Nalu.Maui.Navigation/NaluShell.cs @@ -1,5 +1,6 @@ namespace Nalu; +using System.Diagnostics; using System.Text.RegularExpressions; #pragma warning disable IDE0290 @@ -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); + } + } } }