From 66ef72712aafdb49c994d8b2316ab4280a19b89b Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 3 May 2026 08:49:34 -0700 Subject: [PATCH] Improve Mark as Failed for non-grabbed history events --- .../Download/FailedDownloadService.cs | 17 ++++++++++++++--- .../ErrorManagement/SonarrErrorPipeline.cs | 7 +++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/Download/FailedDownloadService.cs b/src/NzbDrone.Core/Download/FailedDownloadService.cs index 37e05e099..c1d383ce8 100644 --- a/src/NzbDrone.Core/Download/FailedDownloadService.cs +++ b/src/NzbDrone.Core/Download/FailedDownloadService.cs @@ -40,9 +40,13 @@ public void MarkAsFailed(int historyId, string message, string source = null, bo if (downloadId.IsNullOrWhiteSpace()) { - PublishDownloadFailedEvent(history, new List { history.EpisodeId }, message, source, skipRedownload: skipRedownload); + if (history.EventType == EpisodeHistoryEventType.Grabbed) + { + PublishDownloadFailedEvent(history, new List { history.EpisodeId }, message, source, skipRedownload: skipRedownload); + return; + } - return; + throw new InvalidOperationException("Unable to mark download as failed, history item was not grabbed and has no download ID"); } var grabbedHistory = new List(); @@ -57,7 +61,12 @@ public void MarkAsFailed(int historyId, string message, string source = null, bo grabbedHistory.AddRange(GetGrabbedHistory(downloadId)); grabbedHistory = grabbedHistory.DistinctBy(h => h.Id).ToList(); - PublishDownloadFailedEvent(history, GetEpisodeIds(grabbedHistory), message, source); + if (grabbedHistory.Any()) + { + PublishDownloadFailedEvent(grabbedHistory.First(), GetEpisodeIds(grabbedHistory), message, source); + } + + throw new InvalidOperationException("Unable to mark download as failed, no grabbed history available"); } public void MarkAsFailed(TrackedDownload trackedDownload, string message, string source = null, bool skipRedownload = false) @@ -68,6 +77,8 @@ public void MarkAsFailed(TrackedDownload trackedDownload, string message, string { PublishDownloadFailedEvent(history.First(), GetEpisodeIds(history), message ?? "Manually marked as failed", source, trackedDownload, skipRedownload: skipRedownload); } + + throw new InvalidOperationException("Unable to mark download as failed, no grabbed history available"); } public void Check(TrackedDownload trackedDownload) diff --git a/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs b/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs index 32786e725..e232f3eca 100644 --- a/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs +++ b/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs @@ -1,3 +1,4 @@ +using System; using System.Data.SQLite; using System.Net; using System.Threading.Tasks; @@ -76,6 +77,12 @@ public async Task HandleException(HttpContext context) _logger.Error(sqLiteException, "[{0} {1}]", context.Request.Method, context.Request.Path); } + else if (exception is InvalidOperationException invalidOperationException) + { + _logger.Warn(invalidOperationException, "[{0} {1}]", context.Request.Method, context.Request.Path); + + statusCode = HttpStatusCode.BadRequest; + } else { _logger.Fatal(exception, "Request Failed. {0} {1}", context.Request.Method, context.Request.Path);