Skip to content

Code Quality: Allow multiple exception types to be ignored #17359

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions src/Files.App/Extensions/DispatcherQueueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static Task EnqueueOrInvokeAsync(this DispatcherQueue? dispatcher, Func<T
}

return function();
}, App.Logger, typeof(COMException));
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
}

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

return function();
}, App.Logger, typeof(COMException));
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
}

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

function();
return Task.CompletedTask;
}, App.Logger, typeof(COMException));
}, App.Logger, typeof(COMException), typeof(InvalidOperationException));
}

public static Task<T?> EnqueueOrInvokeAsync<T>(this DispatcherQueue? dispatcher, Func<T> function, DispatcherQueuePriority priority = DispatcherQueuePriority.Normal)
Expand All @@ -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));
}

}
Expand Down
88 changes: 68 additions & 20 deletions src/Files.Shared/Extensions/SafetyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
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
{
Expand All @@ -18,18 +18,30 @@
}
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)

Check warning on line 24 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Dereference of a possibly null reference.

Check warning on line 24 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Dereference of a possibly null reference.

Check warning on line 24 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Dereference of a possibly null reference.

Check warning on line 24 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Dereference of a possibly null reference.
{
if (type.IsAssignableFrom(ex.GetType()))
{
shouldIgnore = true;
break;
}
}
}

if (shouldIgnore)
{
logger?.LogInformation(ex, ex.Message);
return false;
}
else
throw;

throw;
}
}

public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null, Type? exceptionToIgnore = null)
public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
{
try
{
Expand All @@ -39,52 +51,88 @@
}
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)

Check warning on line 57 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Dereference of a possibly null reference.

Check warning on line 57 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Dereference of a possibly null reference.

Check warning on line 57 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Dereference of a possibly null reference.

Check warning on line 57 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Dereference of a possibly null reference.
{
if (type.IsAssignableFrom(ex.GetType()))
{
shouldIgnore = true;
break;
}
}
}

if (shouldIgnore)
{
logger?.LogInformation(ex, ex.Message);
return false;
}
else
throw;

throw;
}
}

public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null, Type? exceptionToIgnore = null)
public static T? IgnoreExceptions<T>(Func<T> action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
{
try
{
return action();
}
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)

Check warning on line 88 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Dereference of a possibly null reference.

Check warning on line 88 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Dereference of a possibly null reference.

Check warning on line 88 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Dereference of a possibly null reference.

Check warning on line 88 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Dereference of a possibly null reference.
{
if (type.IsAssignableFrom(ex.GetType()))
{
shouldIgnore = true;
break;
}
}
}

if (shouldIgnore)
{
logger?.LogInformation(ex, ex.Message);
return default;
}
else
throw;

throw;
}
}

public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null, Type? exceptionToIgnore = null)
public static async Task<T?> IgnoreExceptions<T>(Func<Task<T>> action, ILogger? logger = null, params Type[]? exceptionsToIgnore)
{
try
{
return await action();
}
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)

Check warning on line 119 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Dereference of a possibly null reference.

Check warning on line 119 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Dereference of a possibly null reference.

Check warning on line 119 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Dereference of a possibly null reference.

Check warning on line 119 in src/Files.Shared/Extensions/SafetyExtensions.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Dereference of a possibly null reference.
{
if (type.IsAssignableFrom(ex.GetType()))
{
shouldIgnore = true;
break;
}
}
}

if (shouldIgnore)
{
logger?.LogInformation(ex, ex.Message);
return default;
}
else
throw;

throw;
}
}

Expand Down
Loading