Skip to content

Commit 4c3128f

Browse files
Code Quality: Allow multiple exception types to be ignored
1 parent ec7cfdc commit 4c3128f

File tree

2 files changed

+72
-24
lines changed

2 files changed

+72
-24
lines changed

src/Files.App/Extensions/DispatcherQueueExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func<T
2626
}
2727

2828
return function();
29-
}, App.Logger, typeof(COMException));
29+
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
3030
}
3131

3232
public static Task<T?> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<Task<T>> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
@@ -47,7 +47,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func<T
4747
}
4848

4949
return function();
50-
}, App.Logger, typeof(COMException));
50+
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
5151
}
5252

5353
public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
@@ -69,7 +69,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action
6969

7070
function();
7171
return Task.CompletedTask;
72-
}, App.Logger, typeof(COMException));
72+
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
7373
}
7474

7575
public static Task<T?> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<T> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
@@ -90,7 +90,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action
9090
}
9191

9292
return Task.FromResult(function());
93-
}, App.Logger, typeof(COMException));
93+
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
9494
}
9595

9696
}

src/Files.Shared/Extensions/SafetyExtensions.cs

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Files.Shared.Extensions
99
{
1010
public static class SafetyExtensions
1111
{
12-
public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type? exceptionToIgnore = null)
12+
public static bool IgnoreExceptions(Action action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
1313
{
1414
try
1515
{
@@ -18,18 +18,30 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type?
1818
}
1919
catch (Exception ex)
2020
{
21-
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
21+
bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0;
22+
if (!shouldIgnore)
2223
{
23-
logger?.LogInformation(ex, ex.Message);
24+
foreach (var type in exceptionsToIgnore)
25+
{
26+
if (type.IsAssignableFrom(ex.GetType()))
27+
{
28+
shouldIgnore = true;
29+
break;
30+
}
31+
}
32+
}
2433

34+
if (shouldIgnore)
35+
{
36+
logger?.LogInformation(ex, ex.Message);
2537
return false;
2638
}
27-
else
28-
throw;
39+
40+
throw;
2941
}
3042
}
3143

32-
public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null, Type? exceptionToIgnore = null)
44+
public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
3345
{
3446
try
3547
{
@@ -39,52 +51,88 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
3951
}
4052
catch (Exception ex)
4153
{
42-
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
54+
bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0;
55+
if (!shouldIgnore)
4356
{
44-
logger?.LogInformation(ex, ex.Message);
57+
foreach (var type in exceptionsToIgnore)
58+
{
59+
if (type.IsAssignableFrom(ex.GetType()))
60+
{
61+
shouldIgnore = true;
62+
break;
63+
}
64+
}
65+
}
4566

67+
if (shouldIgnore)
68+
{
69+
logger?.LogInformation(ex, ex.Message);
4670
return false;
4771
}
48-
else
49-
throw;
72+
73+
throw;
5074
}
5175
}
5276

53-
public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null, Type? exceptionToIgnore = null)
77+
public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
5478
{
5579
try
5680
{
5781
return action();
5882
}
5983
catch (Exception ex)
6084
{
61-
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
85+
bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0;
86+
if (!shouldIgnore)
6287
{
63-
logger?.LogInformation(ex, ex.Message);
88+
foreach (var type in exceptionsToIgnore)
89+
{
90+
if (type.IsAssignableFrom(ex.GetType()))
91+
{
92+
shouldIgnore = true;
93+
break;
94+
}
95+
}
96+
}
6497

98+
if (shouldIgnore)
99+
{
100+
logger?.LogInformation(ex, ex.Message);
65101
return default;
66102
}
67-
else
68-
throw;
103+
104+
throw;
69105
}
70106
}
71107

72-
public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null, Type? exceptionToIgnore = null)
108+
public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
73109
{
74110
try
75111
{
76112
return await action();
77113
}
78114
catch (Exception ex)
79115
{
80-
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
116+
bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore.Length == 0;
117+
if (!shouldIgnore)
81118
{
82-
logger?.LogInformation(ex, ex.Message);
119+
foreach (var type in exceptionsToIgnore)
120+
{
121+
if (type.IsAssignableFrom(ex.GetType()))
122+
{
123+
shouldIgnore = true;
124+
break;
125+
}
126+
}
127+
}
83128

129+
if (shouldIgnore)
130+
{
131+
logger?.LogInformation(ex, ex.Message);
84132
return default;
85133
}
86-
else
87-
throw;
134+
135+
throw;
88136
}
89137
}
90138

0 commit comments

Comments
 (0)