From 66cf79ac140d6d8f3a168ac644c7f4bcc6130cf9 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 10 Oct 2026 21:29:34 -0500 Subject: [PATCH] fix(http): tell a missing snapshot API from its status, not from the error text The error message carries the request path, so a snapshot named nightly404 turned a real server error into 'not available'. FlociException now carries the HTTP status and the snapshot commands ask that. Signed-off-by: Hector Ventura --- .../snapshot/SnapshotDeleteCommand.java | 2 +- .../snapshot/SnapshotListCommand.java | 2 +- .../snapshot/SnapshotLoadCommand.java | 2 +- .../snapshot/SnapshotSaveCommand.java | 2 +- .../io/floci/cli/http/FlociException.java | 22 +++++++++++++ .../io/floci/cli/http/FlociHttpClient.java | 4 +-- .../floci/cli/unit/SnapshotCommandsTest.java | 31 ++++++++++++++++++- 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java index 85a1beb..484581c 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java @@ -47,7 +47,7 @@ public class SnapshotDeleteCommand implements Callable { printer.println(Ansi.green("Snapshot deleted:") + " " + name); return 0; } catch (FlociException e) { - if (e.getMessage().contains("404") || e.getMessage().contains("501")) { + if (e.endpointMissing()) { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java index 226aaf4..4ae84af 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java @@ -54,7 +54,7 @@ public class SnapshotListCommand implements Callable { node.forEach(s -> printer.println(" " + s.asText())); return 0; } catch (FlociException e) { - if (e.getMessage().contains("404") || e.getMessage().contains("501")) { + if (e.endpointMissing()) { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java index 9a94732..5436c78 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java @@ -47,7 +47,7 @@ public class SnapshotLoadCommand implements Callable { printer.println(Ansi.green("Snapshot loaded:") + " " + name); return 0; } catch (FlociException e) { - if (e.getMessage().contains("404") || e.getMessage().contains("501")) { + if (e.endpointMissing()) { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java index 666ca4a..1badd36 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java @@ -54,7 +54,7 @@ public class SnapshotSaveCommand implements Callable { printer.println(Ansi.green("Snapshot saved:") + " " + name); return 0; } catch (FlociException e) { - if (e.getMessage().contains("404") || e.getMessage().contains("501")) { + if (e.endpointMissing()) { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { diff --git a/src/main/java/io/floci/cli/http/FlociException.java b/src/main/java/io/floci/cli/http/FlociException.java index 222d7b9..e632093 100644 --- a/src/main/java/io/floci/cli/http/FlociException.java +++ b/src/main/java/io/floci/cli/http/FlociException.java @@ -1,7 +1,29 @@ package io.floci.cli.http; public class FlociException extends Exception { + + private final int status; + public FlociException(String message) { + this(message, 0); + } + + /** An error the server answered with HTTP {@code status}. */ + public FlociException(String message, int status) { super(message); + this.status = status; + } + + /** The HTTP status the server answered with, or 0 when the failure was not an HTTP response. */ + public int status() { + return status; + } + + /** + * The server has no such endpoint (404) or does not implement it (501). Asked of the status, + * never of the message: the message carries the request path, and a path can contain "404". + */ + public boolean endpointMissing() { + return status == 404 || status == 501; } } diff --git a/src/main/java/io/floci/cli/http/FlociHttpClient.java b/src/main/java/io/floci/cli/http/FlociHttpClient.java index 41cb23b..def58c9 100644 --- a/src/main/java/io/floci/cli/http/FlociHttpClient.java +++ b/src/main/java/io/floci/cli/http/FlociHttpClient.java @@ -164,7 +164,7 @@ public class FlociHttpClient { message.contains("not available yet")); } if (resp.statusCode() >= 400) { - throw new FlociException("Server returned HTTP " + resp.statusCode() + " for " + path); + throw new FlociException("Server returned HTTP " + resp.statusCode() + " for " + path, resp.statusCode()); } return resp.body(); } catch (FlociException e) { @@ -295,7 +295,7 @@ public class FlociHttpClient { + "Point --endpoint at the address Floci listens on."); } if (status >= 400) { - throw new FlociException("Server returned HTTP " + status + " for " + path); + throw new FlociException("Server returned HTTP " + status + " for " + path, status); } } diff --git a/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java b/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java index 44fc7d2..1bd3f05 100644 --- a/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java +++ b/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java @@ -7,6 +7,8 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -22,12 +24,13 @@ class SnapshotCommandsTest { Path tempDir; private HttpServer server; + private volatile int status = 404; @BeforeEach void start() throws Exception { server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); server.createContext("/", exchange -> { - exchange.sendResponseHeaders(404, -1); + exchange.sendResponseHeaders(status, -1); exchange.close(); }); server.start(); @@ -74,4 +77,30 @@ class SnapshotCommandsTest { assertTrue(r[2].contains("--message is not sent"), r[2]); } + + /** + * Only the status says the API is missing. A name that happens to contain "404" or "501" must + * not turn a real server error into "not available". + */ + @ParameterizedTest + @CsvSource({"delete, nightly404", "save, build-501", "load, 404"}) + void aStatusLookingNameDoesNotHideARealError(String command, String name) { + status = 500; + + String[] r = run("snapshot", command, name); + + assertEquals("1", r[0]); + assertFalse(r[2].contains("Snapshot API not available"), r[2]); + assertTrue(r[2].contains("HTTP 500"), r[2]); + } + + @ParameterizedTest + @CsvSource({"404", "501"}) + void bothMissingEndpointStatusesReadAsNotAvailable(int missing) { + status = missing; + + String[] r = run("snapshot", "delete", "nightly"); + + assertTrue(r[2].contains("Snapshot API not available"), r[2]); + } }