diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-27 19:14:30 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-27 19:14:30 -0600 |
| commit | cd5def0cc84adfa03ba50cddb07e5ed29108ca34 (patch) | |
| tree | b801cf2a60606ff30de4be0c762261718980f05a /src/slskd | |
| parent | 60520cdb16f8a01ed753ee1905053d9ec99a9950 (diff) | |
| download | slskd-cd5def0cc84adfa03ba50cddb07e5ed29108ca34.tar.gz | |
add TryFail
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 52 |
1 files changed, 52 insertions, 0 deletions
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 @@ -126,6 +126,20 @@ namespace slskd.Transfers.Uploads bool TryCancel(Guid id); /// <summary> + /// Fails the upload matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <param name="id">The unique identifier for the upload.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the upload was successfully failed.</returns> + bool TryFail(Guid id, Exception exception); + + /// <summary> /// Synchronously updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> @@ -899,6 +913,44 @@ namespace slskd.Transfers.Uploads } /// <summary> + /// Fails the upload matching the specified <paramref name="id"/> with the specified <paramref name="exception"/>, + /// and sets the final state accordingly. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <param name="id">The unique identifier for the upload.</param> + /// <param name="exception">The exception that caused the failure.</param> + /// <returns>A value indicating whether the upload was successfully failed.</returns> + 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; + } + + /// <summary> /// Synchronously updates the specified <paramref name="transfer"/>. /// </summary> /// <param name="transfer">The transfer to update.</param> |
