Skip to content

Fix: Fixed COMException in ItemsAdded_CollectionChanged - part 2 #14448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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