From 8bf205f171382aaff2f14104af1550b57a4bd512 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Thu, 28 Sep 2023 12:18:10 -0500 Subject: [PATCH] fix: Properly rethrow non-deterministic exceptions Doing `throw ` causes the stack trace information to be rewritten (we don't want this). Wrapping in a new exception object like `AggregateException` fixes that but also makes the printed exception look messier. Instead, simply return a bool to indicate if `HandleException()` processed the the exception; if not, then we can rethrow from outside the method. --- .../Console/Commands/DeleteCustomFormatsCommand.cs | 7 ++++++- .../ErrorHandling/ConsoleExceptionHandler.cs | 7 ++++--- src/Recyclarr.Cli/Processors/FatalException.cs | 4 ++-- src/Recyclarr.Cli/Processors/Sync/SyncProcessor.cs | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Recyclarr.Cli/Console/Commands/DeleteCustomFormatsCommand.cs b/src/Recyclarr.Cli/Console/Commands/DeleteCustomFormatsCommand.cs index 13b81479..a3ce5f93 100644 --- a/src/Recyclarr.Cli/Console/Commands/DeleteCustomFormatsCommand.cs +++ b/src/Recyclarr.Cli/Console/Commands/DeleteCustomFormatsCommand.cs @@ -61,7 +61,12 @@ public class DeleteCustomFormatsCommand : AsyncCommand HandleException(Exception sourceException) { switch (sourceException) { @@ -65,9 +65,10 @@ public class ConsoleExceptionHandler _log.Error(e.Message); break; - // This handles non-deterministic/unexpected exceptions. default: - throw sourceException; + return false; } + + return true; } } diff --git a/src/Recyclarr.Cli/Processors/FatalException.cs b/src/Recyclarr.Cli/Processors/FatalException.cs index 3d249846..95e69a03 100644 --- a/src/Recyclarr.Cli/Processors/FatalException.cs +++ b/src/Recyclarr.Cli/Processors/FatalException.cs @@ -6,8 +6,8 @@ namespace Recyclarr.Cli.Processors; [Serializable] public class FatalException : Exception { - public FatalException(string? message) - : base(message) + public FatalException(string? message, Exception? innerException = null) + : base(message, innerException) { } diff --git a/src/Recyclarr.Cli/Processors/Sync/SyncProcessor.cs b/src/Recyclarr.Cli/Processors/Sync/SyncProcessor.cs index c8cd6a02..d05c9254 100644 --- a/src/Recyclarr.Cli/Processors/Sync/SyncProcessor.cs +++ b/src/Recyclarr.Cli/Processors/Sync/SyncProcessor.cs @@ -51,7 +51,12 @@ public class SyncProcessor : ISyncProcessor } catch (Exception e) { - await _exceptionHandler.HandleException(e); + if (!await _exceptionHandler.HandleException(e)) + { + // This means we didn't handle the exception; rethrow it. + throw; + } + failureDetected = true; } @@ -73,7 +78,12 @@ public class SyncProcessor : ISyncProcessor } catch (Exception e) { - await _exceptionHandler.HandleException(e); + if (!await _exceptionHandler.HandleException(e)) + { + // This means we didn't handle the exception; rethrow it. + throw; + } + failureDetected = true; } }