From 4c3128f44ff245d8c6c3630feaf1662aceb11a25 Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Wed, 30 Jul 2025 01:33:52 +0000 Subject: [PATCH] Code Quality: Allow multiple exception types to be ignored --- .../Extensions/DispatcherQueueExtensions.cs | 8 +- .../Extensions/SafetyExtensions.cs | 88 ++++++++++++++----- 2 files changed, 72 insertions(+), 24 deletions(-) diff --git a/src/Files.App/Extensions/DispatcherQueueExtensions.cs b/src/Files.App/Extensions/DispatcherQueueExtensions.cs index 346da986b1c1..dd0858c26161 100644 --- a/src/Files.App/Extensions/DispatcherQueueExtensions.cs +++ b/src/Files.App/Extensions/DispatcherQueueExtensions.cs @@ -26,7 +26,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) @@ -47,7 +47,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal) @@ -90,7 +90,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action } return Task.FromResult(function()); - }, App.Logger, typeof(COMException)); + }, App.Logger, typeof(COMException), typeof(InvalidOperationException)); } } diff --git a/src/Files.Shared/Extensions/SafetyExtensions.cs b/src/Files.Shared/Extensions/SafetyExtensions.cs index 75a7467438df..7034bdff5789 100644 --- a/src/Files.Shared/Extensions/SafetyExtensions.cs +++ b/src/Files.Shared/Extensions/SafetyExtensions.cs @@ -9,7 +9,7 @@ namespace Files.Shared.Extensions { public static class SafetyExtensions { - public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type? exceptionToIgnore = null) + public static bool IgnoreExceptions(Action action, ILogger? logger = null, params Type[]? exceptionsToIgnore) { try { @@ -18,18 +18,30 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type? } catch (Exception ex) { - if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType())) + bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0; + if (!shouldIgnore) { - logger?.LogInformation(ex, ex.Message); + foreach (var type in exceptionsToIgnore) + { + if (type.IsAssignableFrom(ex.GetType())) + { + shouldIgnore = true; + break; + } + } + } + if (shouldIgnore) + { + logger?.LogInformation(ex, ex.Message); return false; } - else - throw; + + throw; } } - public static async Task IgnoreExceptions(Func action, ILogger? logger = null, Type? exceptionToIgnore = null) + public static async Task IgnoreExceptions(Func action, ILogger? logger = null, params Type[]? exceptionsToIgnore) { try { @@ -39,18 +51,30 @@ public static async Task IgnoreExceptions(Func action, ILogger? logg } catch (Exception ex) { - if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType())) + bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0; + if (!shouldIgnore) { - logger?.LogInformation(ex, ex.Message); + foreach (var type in exceptionsToIgnore) + { + if (type.IsAssignableFrom(ex.GetType())) + { + shouldIgnore = true; + break; + } + } + } + if (shouldIgnore) + { + logger?.LogInformation(ex, ex.Message); return false; } - else - throw; + + throw; } } - public static T? IgnoreExceptions(Func action, ILogger? logger = null, Type? exceptionToIgnore = null) + public static T? IgnoreExceptions(Func action, ILogger? logger = null, params Type[]? exceptionsToIgnore) { try { @@ -58,18 +82,30 @@ public static async Task IgnoreExceptions(Func action, ILogger? logg } catch (Exception ex) { - if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType())) + bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0; + if (!shouldIgnore) { - logger?.LogInformation(ex, ex.Message); + foreach (var type in exceptionsToIgnore) + { + if (type.IsAssignableFrom(ex.GetType())) + { + shouldIgnore = true; + break; + } + } + } + if (shouldIgnore) + { + logger?.LogInformation(ex, ex.Message); return default; } - else - throw; + + throw; } } - public static async Task IgnoreExceptions(Func> action, ILogger? logger = null, Type? exceptionToIgnore = null) + public static async Task IgnoreExceptions(Func> action, ILogger? logger = null, params Type[]? exceptionsToIgnore) { try { @@ -77,14 +113,26 @@ public static async Task IgnoreExceptions(Func action, ILogger? logg } catch (Exception ex) { - if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType())) + bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0; + if (!shouldIgnore) { - logger?.LogInformation(ex, ex.Message); + foreach (var type in exceptionsToIgnore) + { + if (type.IsAssignableFrom(ex.GetType())) + { + shouldIgnore = true; + break; + } + } + } + if (shouldIgnore) + { + logger?.LogInformation(ex, ex.Message); return default; } - else - throw; + + throw; } }