mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-12-05 11:08:00 -05:00
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 <hectorvent@gmail.com>
This commit is contained in:
7 files changed
+58
-7
No files matched your search
@@ -47,7 +47,7 @@ public class SnapshotDeleteCommand implements Callable<Integer> {
|
||||
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 {
|
||||
|
||||
@@ -54,7 +54,7 @@ public class SnapshotListCommand implements Callable<Integer> {
|
||||
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 {
|
||||
|
||||
@@ -47,7 +47,7 @@ public class SnapshotLoadCommand implements Callable<Integer> {
|
||||
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 {
|
||||
|
||||
@@ -54,7 +54,7 @@ public class SnapshotSaveCommand implements Callable<Integer> {
|
||||
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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user