Skip to content

Commit 4ef4730

Browse files
authored
Fix: Fixed COMException in ItemsAdded_CollectionChanged - part 2 (#14448)
1 parent bb82313 commit 4ef4730

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/Files.App/Extensions/DispatcherQueueExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using CommunityToolkit.WinUI;
22
using Microsoft.UI.Dispatching;
3-
using System;
4-
using System.Threading.Tasks;
3+
using System.Runtime.InteropServices;
54

65
namespace Files.App.Extensions
76
{
@@ -14,15 +13,15 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func<T
1413
if (dispatcher is not null)
1514
return dispatcher.EnqueueAsync(function, priority);
1615
else
17-
return function();
16+
return SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException));
1817
}
1918

2019
public static Task<T> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<Task<T>> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
2120
{
2221
if (dispatcher is not null)
2322
return dispatcher.EnqueueAsync(function, priority);
2423
else
25-
return function();
24+
return SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException));
2625
}
2726

2827
public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
@@ -31,7 +30,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action
3130
return dispatcher.EnqueueAsync(function, priority);
3231
else
3332
{
34-
function();
33+
SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException));
3534
return Task.CompletedTask;
3635
}
3736
}
@@ -41,7 +40,7 @@ public static Task<T> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher,
4140
if (dispatcher is not null)
4241
return dispatcher.EnqueueAsync(function, priority);
4342
else
44-
return Task.FromResult(function());
43+
return Task.FromResult(SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException)));
4544
}
4645

4746
}

src/Files.Shared/Extensions/SafetyExtensions.cs

Lines changed: 32 additions & 12 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)
12+
public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type? exceptionToIgnore = null)
1313
{
1414
try
1515
{
@@ -18,13 +18,18 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null)
1818
}
1919
catch (Exception ex)
2020
{
21-
logger?.LogInformation(ex, ex.Message);
21+
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
22+
{
23+
logger?.LogInformation(ex, ex.Message);
2224

23-
return false;
25+
return false;
26+
}
27+
else
28+
throw;
2429
}
2530
}
2631

27-
public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null)
32+
public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null, Type? exceptionToIgnore = null)
2833
{
2934
try
3035
{
@@ -34,37 +39,52 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
3439
}
3540
catch (Exception ex)
3641
{
37-
logger?.LogInformation(ex, ex.Message);
42+
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
43+
{
44+
logger?.LogInformation(ex, ex.Message);
3845

39-
return false;
46+
return false;
47+
}
48+
else
49+
throw;
4050
}
4151
}
4252

43-
public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null)
53+
public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null, Type? exceptionToIgnore = null)
4454
{
4555
try
4656
{
4757
return action();
4858
}
4959
catch (Exception ex)
5060
{
51-
logger?.LogInformation(ex, ex.Message);
61+
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
62+
{
63+
logger?.LogInformation(ex, ex.Message);
5264

53-
return default;
65+
return default;
66+
}
67+
else
68+
throw;
5469
}
5570
}
5671

57-
public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null)
72+
public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null, Type? exceptionToIgnore = null)
5873
{
5974
try
6075
{
6176
return await action();
6277
}
6378
catch (Exception ex)
6479
{
65-
logger?.LogInformation(ex, ex.Message);
80+
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
81+
{
82+
logger?.LogInformation(ex, ex.Message);
6683

67-
return default;
84+
return default;
85+
}
86+
else
87+
throw;
6888
}
6989
}
7090

0 commit comments

Comments
 (0)