From d7ca2b575777a6020443825eb5f4e6a5a3938238 Mon Sep 17 00:00:00 2001 From: campersau Date: Wed, 4 Mar 2026 08:44:26 +0100 Subject: [PATCH] Use typed exception for node not in swarm and node already in swarm --- .../DockerSwarmNodeAlreadyParticipatingException.cs | 9 +++++++++ .../DockerSwarmNodeNotParticipatingException.cs | 9 +++++++++ src/Docker.DotNet/Endpoints/SwarmOperations.cs | 6 ++---- test/Docker.DotNet.Tests/TestFixture.cs | 2 +- 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/Docker.DotNet/Endpoints/DockerSwarmNodeAlreadyParticipatingException.cs create mode 100644 src/Docker.DotNet/Endpoints/DockerSwarmNodeNotParticipatingException.cs diff --git a/src/Docker.DotNet/Endpoints/DockerSwarmNodeAlreadyParticipatingException.cs b/src/Docker.DotNet/Endpoints/DockerSwarmNodeAlreadyParticipatingException.cs new file mode 100644 index 00000000..0b6e76d4 --- /dev/null +++ b/src/Docker.DotNet/Endpoints/DockerSwarmNodeAlreadyParticipatingException.cs @@ -0,0 +1,9 @@ +namespace Docker.DotNet; + +public class DockerSwarmNodeAlreadyParticipatingException : DockerApiException +{ + public DockerSwarmNodeAlreadyParticipatingException(HttpStatusCode statusCode, string? responseBody) + : base(statusCode, responseBody) + { + } +} \ No newline at end of file diff --git a/src/Docker.DotNet/Endpoints/DockerSwarmNodeNotParticipatingException.cs b/src/Docker.DotNet/Endpoints/DockerSwarmNodeNotParticipatingException.cs new file mode 100644 index 00000000..678dc60b --- /dev/null +++ b/src/Docker.DotNet/Endpoints/DockerSwarmNodeNotParticipatingException.cs @@ -0,0 +1,9 @@ +namespace Docker.DotNet; + +public class DockerSwarmNodeNotParticipatingException : DockerApiException +{ + public DockerSwarmNodeNotParticipatingException(HttpStatusCode statusCode, string? responseBody) + : base(statusCode, responseBody) + { + } +} \ No newline at end of file diff --git a/src/Docker.DotNet/Endpoints/SwarmOperations.cs b/src/Docker.DotNet/Endpoints/SwarmOperations.cs index 8609a63e..6ff57d85 100644 --- a/src/Docker.DotNet/Endpoints/SwarmOperations.cs +++ b/src/Docker.DotNet/Endpoints/SwarmOperations.cs @@ -6,8 +6,7 @@ internal class SwarmOperations : ISwarmOperations { if (statusCode == HttpStatusCode.ServiceUnavailable) { - // TODO: Make typed error. - throw new Exception("Node is not part of a swarm."); + throw new DockerSwarmNodeNotParticipatingException(statusCode, responseBody); } }; @@ -15,8 +14,7 @@ internal class SwarmOperations : ISwarmOperations { if (statusCode == HttpStatusCode.ServiceUnavailable) { - // TODO: Make typed error. - throw new Exception("Node is already part of a swarm."); + throw new DockerSwarmNodeAlreadyParticipatingException(statusCode, responseBody); } }; diff --git a/test/Docker.DotNet.Tests/TestFixture.cs b/test/Docker.DotNet.Tests/TestFixture.cs index 8005b89b..9f3c7280 100644 --- a/test/Docker.DotNet.Tests/TestFixture.cs +++ b/test/Docker.DotNet.Tests/TestFixture.cs @@ -93,7 +93,7 @@ await DockerClient.Images.CreateImageAsync(new ImagesCreateParameters { FromImag _hasInitializedSwarm = true; } - catch + catch (DockerSwarmNodeAlreadyParticipatingException) { this.LogInformation("Couldn't init a new swarm, the node should take part of an existing one.");