Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Added IsCancellationException() helper to ExceptionExtensions.

nzbdav-deva33c0d3cd620
diff --git a/backend/Clients/Usenet/BaseNntpClient.cs b/backend/Clients/Usenet/BaseNntpClient.cs
index ccb99cc1..41de8b4f 100644
--- a/backend/Clients/Usenet/BaseNntpClient.cs
+++ b/backend/Clients/Usenet/BaseNntpClient.cs
@@ -1,5 +1,6 @@
using NzbWebDAV.Clients.Usenet.Models;
using NzbWebDAV.Exceptions;
+using NzbWebDAV.Extensions;
using UsenetSharp.Clients;
using UsenetSharp.Models;
using UsenetSharp.Streams;
@@ -23,7 +24,7 @@ public class BaseNntpClient : NntpClient
{
await _client.ConnectAsync(host, port, useSsl, cancellationToken);
}
- catch (Exception e)
+ catch (Exception e) when (!e.IsCancellationException())
{
const string message = "Could not connect to usenet host. Check connection settings.";
throw new CouldNotConnectToUsenetException(message, e);
@@ -48,7 +49,7 @@ public class BaseNntpClient : NntpClient
return response;
}
- catch (Exception e)
+ catch (Exception e) when (!e.IsCancellationException())
{
throw new CouldNotLoginToUsenetException("Could not login to usenet host.", e);
}
diff --git a/backend/Clients/Usenet/MultiProviderNntpClient.cs b/backend/Clients/Usenet/MultiProviderNntpClient.cs
index 54033661..7cd2a018 100644
--- a/backend/Clients/Usenet/MultiProviderNntpClient.cs
+++ b/backend/Clients/Usenet/MultiProviderNntpClient.cs
@@ -1,5 +1,6 @@
using System.Runtime.ExceptionServices;
using NzbWebDAV.Clients.Usenet.Models;
+using NzbWebDAV.Extensions;
using NzbWebDAV.Models;
using Serilog;
using UsenetSharp.Models;
@@ -30,7 +31,7 @@ public class MultiProviderNntpClient(List<MultiConnectionNntpClient> providers)
public override Task<UsenetDecodedBodyResponse> DecodedBodyAsync
(
- SegmentId segmentId,
+ SegmentId segmentId,
CancellationToken cancellationToken
)
{
@@ -147,7 +148,7 @@ public class MultiProviderNntpClient(List<MultiConnectionNntpClient> providers)
return result;
}
- catch (Exception e) when (e is not OperationCanceledException and not TaskCanceledException)
+ catch (Exception e) when (!e.IsCancellationException())
{
lastException = ExceptionDispatchInfo.Capture(e);
}
diff --git a/backend/Extensions/ExceptionExtensions.cs b/backend/Extensions/ExceptionExtensions.cs
index bd811f4f..d9e49a9a 100644
--- a/backend/Extensions/ExceptionExtensions.cs
+++ b/backend/Extensions/ExceptionExtensions.cs
@@ -15,4 +15,9 @@ public static class ExceptionExtensions
or SharpCompress.Common.InvalidFormatException
or Usenet.Exceptions.InvalidYencDataException;
}
+
+ public static bool IsCancellationException(this Exception exception)
+ {
+ return exception is TaskCanceledException or OperationCanceledException;
+ }
}
\ No newline at end of file
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index d1acbe42..460ca992 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -47,7 +47,7 @@ public class QueueItemProcessor(
// When a queue-item is removed while processing,
// then we need to clear any db changes and finish early.
- catch (Exception e) when (e.GetBaseException() is OperationCanceledException or TaskCanceledException)
+ catch (Exception e) when (e.GetBaseException().IsCancellationException())
{
Log.Information($"Processing of queue item `{queueItem.JobName}` was cancelled.");
dbClient.Ctx.ChangeTracker.Clear();
@@ -102,7 +102,8 @@ public class QueueItemProcessor(
if (isDuplicateNzb && duplicateNzbBehavior == "mark-failed")
{
const string error = "Duplicate nzb: the download folder for this nzb already exists.";
- await MarkQueueItemCompleted(startTime, error, () => Task.FromResult(existingMountFolder)).ConfigureAwait(false);
+ await MarkQueueItemCompleted(startTime, error, () => Task.FromResult(existingMountFolder))
+ .ConfigureAwait(false);
return;
}
@@ -168,7 +169,8 @@ public class QueueItemProcessor(
await MarkQueueItemCompleted(startTime, error: null, async () =>
{
var categoryFolder = await GetOrCreateCategoryFolder().ConfigureAwait(false);
- var mountFolder = await CreateMountFolder(categoryFolder, existingMountFolder, duplicateNzbBehavior).ConfigureAwait(false);
+ var mountFolder = await CreateMountFolder(categoryFolder, existingMountFolder, duplicateNzbBehavior)
+ .ConfigureAwait(false);
new RarAggregator(dbClient, mountFolder, checkedFullHealth).UpdateDatabase(fileProcessingResults);
new FileAggregator(dbClient, mountFolder, checkedFullHealth).UpdateDatabase(fileProcessingResults);
new SevenZipAggregator(dbClient, mountFolder, checkedFullHealth).UpdateDatabase(fileProcessingResults);
@@ -184,7 +186,8 @@ public class QueueItemProcessor(
// create strm files, if necessary
if (configManager.GetImportStrategy() == "strm")
- await new CreateStrmFilesPostProcessor(configManager, dbClient).CreateStrmFilesAsync().ConfigureAwait(false);
+ await new CreateStrmFilesPostProcessor(configManager, dbClient).CreateStrmFilesAsync()
+ .ConfigureAwait(false);
return mountFolder;
}).ConfigureAwait(false);
@@ -289,7 +292,8 @@ public class QueueItemProcessor(
for (var i = 2; i < 100; i++)
{
var name = $"{queueItem.JobName} ({i})";
- var existingMountFolder = await dbClient.GetDirectoryChildAsync(categoryFolder.Id, name, ct).ConfigureAwait(false);
+ var existingMountFolder =
+ await dbClient.GetDirectoryChildAsync(categoryFolder.Id, name, ct).ConfigureAwait(false);
if (existingMountFolder is not null) continue;
var mountFolder = DavItem.New(