From cd5def0cc84adfa03ba50cddb07e5ed29108ca34 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Nov 2025 19:14:30 -0600 Subject: add TryFail --- src/slskd/Transfers/Uploads/UploadService.cs | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 4b35bffb..4a3a44a5 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -125,6 +125,20 @@ namespace slskd.Transfers.Uploads /// A value indicating whether the upload was successfully cancelled. bool TryCancel(Guid id); + /// + /// Fails the upload matching the specified with the specified , + /// and sets the final state accordingly. + /// + /// + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// + /// The unique identifier for the upload. + /// The exception that caused the failure. + /// A value indicating whether the upload was successfully failed. + bool TryFail(Guid id, Exception exception); + /// /// Synchronously updates the specified . /// @@ -898,6 +912,44 @@ namespace slskd.Transfers.Uploads return false; } + /// + /// Fails the upload matching the specified with the specified , + /// and sets the final state accordingly. + /// + /// + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// + /// The unique identifier for the upload. + /// The exception that caused the failure. + /// A value indicating whether the upload was successfully failed. + public bool TryFail(Guid id, Exception exception) + { + var t = Find(t => t.Id == id); + + if (t is not null) + { + t.EndedAt ??= DateTime.UtcNow; + t.Exception ??= exception.Message; + + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | exception switch + { + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + } + + Update(t); + return true; + } + + return false; + } + /// /// Synchronously updates the specified . /// -- cgit v1.2.3