Skip to content

Commit

Permalink
Fix: Fixed COMException in ItemsAdded_CollectionChanged - part 2 (fil…
Browse files Browse the repository at this point in the history
  • Loading branch information
hishitetsu authored Jan 14, 2024
1 parent bb82313 commit 4ef4730
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/Files.App/Extensions/DispatcherQueueExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CommunityToolkit.WinUI;
using Microsoft.UI.Dispatching;
using System;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace Files.App.Extensions
{
Expand All @@ -14,15 +13,15 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func<T
if (dispatcher is not null)
return dispatcher.EnqueueAsync(function, priority);
else
return function();
return SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException));
}

public static Task<T> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<Task<T>> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
{
if (dispatcher is not null)
return dispatcher.EnqueueAsync(function, priority);
else
return function();
return SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException));
}

public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
Expand All @@ -31,7 +30,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Action
return dispatcher.EnqueueAsync(function, priority);
else
{
function();
SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException));
return Task.CompletedTask;
}
}
Expand All @@ -41,7 +40,7 @@ public static Task<T> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher,
if (dispatcher is not null)
return dispatcher.EnqueueAsync(function, priority);
else
return Task.FromResult(function());
return Task.FromResult(SafetyExtensions.IgnoreExceptions(function, App.Logger, typeof(COMException)));
}

}
Expand Down
44 changes: 32 additions & 12 deletions src/Files.Shared/Extensions/SafetyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Files.Shared.Extensions
{
public static class SafetyExtensions
{
public static bool IgnoreExceptions(Action action, ILogger? logger = null)
public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type? exceptionToIgnore = null)
{
try
{
Expand All @@ -18,13 +18,18 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null)
}
catch (Exception ex)
{
logger?.LogInformation(ex, ex.Message);
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
{
logger?.LogInformation(ex, ex.Message);

return false;
return false;
}
else
throw;
}
}

public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null)
public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null, Type? exceptionToIgnore = null)
{
try
{
Expand All @@ -34,37 +39,52 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
}
catch (Exception ex)
{
logger?.LogInformation(ex, ex.Message);
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
{
logger?.LogInformation(ex, ex.Message);

return false;
return false;
}
else
throw;
}
}

public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null)
public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null, Type? exceptionToIgnore = null)
{
try
{
return action();
}
catch (Exception ex)
{
logger?.LogInformation(ex, ex.Message);
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
{
logger?.LogInformation(ex, ex.Message);

return default;
return default;
}
else
throw;
}
}

public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null)
public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null, Type? exceptionToIgnore = null)
{
try
{
return await action();
}
catch (Exception ex)
{
logger?.LogInformation(ex, ex.Message);
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
{
logger?.LogInformation(ex, ex.Message);

return default;
return default;
}
else
throw;
}
}

Expand Down

0 comments on commit 4ef4730

Please sign in to comment.