From bd4fa0f1343c38eac3a1e5ef551a724816dd022d Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 10 Oct 2026 23:02:30 -0500 Subject: [PATCH] fix(cli): end every error message with what to do next (#58) * fix(cli): end every error message with what to do next Docker, snapshot, update and config profile errors now say how to recover (check Docker, run doctor or status, fix or delete the profile file, pass the missing profile name) instead of stopping at the failure. A test keeps new error messages to the convention. Signed-off-by: Hector Ventura * fix(cli): keep the next step after docker's own output and name the instance in snapshot hints Docker's error output ends in a newline and can span lines, which read as 'already has a next step' and hid the hint. The exception now knows whether its text is docker's output or the CLI's own advice. Snapshot hints select the same instance again. Signed-off-by: Hector Ventura --------- Signed-off-by: Hector Ventura --- .../io/floci/cli/commands/LogsCommand.java | 4 +- .../io/floci/cli/commands/ResetCommand.java | 5 +- .../io/floci/cli/commands/StartCommand.java | 9 +- .../io/floci/cli/commands/StopCommand.java | 6 +- .../io/floci/cli/commands/UpdateCommand.java | 4 +- .../config/ConfigDefaultProductCommand.java | 5 +- .../commands/config/ConfigProfileCommand.java | 28 ++++--- .../config/ConfigValidateCommand.java | 5 +- .../snapshot/SnapshotDeleteCommand.java | 3 +- .../snapshot/SnapshotListCommand.java | 3 +- .../snapshot/SnapshotLoadCommand.java | 3 +- .../snapshot/SnapshotSaveCommand.java | 3 +- .../io/floci/cli/docker/DockerClient.java | 5 +- .../io/floci/cli/docker/DockerException.java | 23 ++++++ .../java/io/floci/cli/output/Printer.java | 10 +++ .../cli/unit/ErrorHintConventionTest.java | 73 +++++++++++++++++ .../io/floci/cli/unit/PrinterErrorTest.java | 82 +++++++++++++++++++ .../floci/cli/unit/SnapshotCommandsTest.java | 12 +++ 18 files changed, 252 insertions(+), 31 deletions(-) create mode 100644 src/test/java/io/floci/cli/unit/ErrorHintConventionTest.java create mode 100644 src/test/java/io/floci/cli/unit/PrinterErrorTest.java diff --git a/src/main/java/io/floci/cli/commands/LogsCommand.java b/src/main/java/io/floci/cli/commands/LogsCommand.java index 84f6594..b2ad0f2 100644 --- a/src/main/java/io/floci/cli/commands/LogsCommand.java +++ b/src/main/java/io/floci/cli/commands/LogsCommand.java @@ -54,7 +54,7 @@ public class LogsCommand implements Callable { return 1; } } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); + printer.error("Failed to inspect container: " + e.withNextStep(DockerClient.CHECK_DOCKER)); return 1; } @@ -66,7 +66,7 @@ public class LogsCommand implements Callable { docker.streamLogs(global.container, follow, tail, since); return 0; } catch (DockerException e) { - printer.error("Failed to fetch logs: " + e.getMessage()); + printer.error("Failed to fetch logs: " + e.withNextStep(DockerClient.CHECK_DOCKER)); return 1; } } diff --git a/src/main/java/io/floci/cli/commands/ResetCommand.java b/src/main/java/io/floci/cli/commands/ResetCommand.java index 5e4de19..5018750 100644 --- a/src/main/java/io/floci/cli/commands/ResetCommand.java +++ b/src/main/java/io/floci/cli/commands/ResetCommand.java @@ -152,10 +152,9 @@ public class ResetCommand implements Callable { printer.error("Resetting state is not available on this " + profile.displayName() + " server version.\n" + "Restart it without --persist for a clean state, or track progress: " + profile.serverRepo() + "/issues"); } else { - String message = "Failed to reset " + profile.displayName() + ": " + e.getMessage(); // Connection errors already say what to do; others get the status hint. - printer.error(message.contains("\n") ? message - : message + "\nRun '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator."); + printer.error("Failed to reset " + profile.displayName() + ": " + e.getMessage(), + "Run '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator."); } return 1; } diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 4d79b83..7ab7887 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -331,7 +331,11 @@ public class StartCommand implements Callable { printer.error("docker binary not found in PATH.\nInstall Docker Desktop from https://docs.docker.com/get-docker/"); return 1; } - printer.error("Failed to inspect container: " + e.getMessage() + socketGuidance(daemon)); + // The socket guidance, when there is any, is the next step; otherwise the general one. + String guidance = socketGuidance(daemon); + printer.error("Failed to inspect container: " + (guidance.isEmpty() + ? e.withNextStep(DockerClient.CHECK_DOCKER) + : e.getMessage().strip() + guidance)); return 1; } @@ -360,7 +364,8 @@ public class StartCommand implements Callable { String id = docker.startContainer(args); printer.println(Ansi.green("Container started") + " (" + id.substring(0, Math.min(12, id.length())) + ")"); } catch (DockerException e) { - printer.error("Failed to start container: " + e.getMessage()); + printer.error("Failed to start container: " + e.getMessage(), + "Run '" + profile.commandPrefix() + " doctor' to check Docker, the image and the port."); return 1; } diff --git a/src/main/java/io/floci/cli/commands/StopCommand.java b/src/main/java/io/floci/cli/commands/StopCommand.java index 87f2749..1af7136 100644 --- a/src/main/java/io/floci/cli/commands/StopCommand.java +++ b/src/main/java/io/floci/cli/commands/StopCommand.java @@ -57,7 +57,7 @@ public class StopCommand implements Callable { return 0; } } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); + printer.error("Failed to inspect container: " + e.withNextStep(DockerClient.CHECK_DOCKER)); return 1; } @@ -66,7 +66,7 @@ public class StopCommand implements Callable { docker.stopContainer(global.container, timeout); printer.println(Ansi.green("Stopped.")); } catch (DockerException e) { - printer.error("Failed to stop container: " + e.getMessage()); + printer.error("Failed to stop container: " + e.withNextStep(DockerClient.CHECK_DOCKER)); return 1; } @@ -75,7 +75,7 @@ public class StopCommand implements Callable { docker.removeContainer(global.container); printer.println("Container removed."); } catch (DockerException e) { - printer.error("Failed to remove container: " + e.getMessage()); + printer.error("Failed to remove container: " + e.withNextStep(DockerClient.CHECK_DOCKER)); return 1; } } diff --git a/src/main/java/io/floci/cli/commands/UpdateCommand.java b/src/main/java/io/floci/cli/commands/UpdateCommand.java index 990ecf3..1f0f2b6 100644 --- a/src/main/java/io/floci/cli/commands/UpdateCommand.java +++ b/src/main/java/io/floci/cli/commands/UpdateCommand.java @@ -142,10 +142,10 @@ public class UpdateCommand implements Callable { + Ansi.gray(" (" + binary + ")")); return 0; } catch (UpdateException e) { - printer.error(e.getMessage()); + printer.error(e.getMessage(), "To reinstall instead, see https://floci.io."); return check ? 2 : 1; // --check keeps 1 for "update available" } catch (Exception e) { - printer.error("update failed: " + e.getMessage() + "\nCheck your network connection and re-run 'floci update'."); + printer.error("update failed: " + e.getMessage(), "Check your network connection and re-run 'floci update'."); return check ? 2 : 1; } } diff --git a/src/main/java/io/floci/cli/commands/config/ConfigDefaultProductCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigDefaultProductCommand.java index 9fe172c..28343d8 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigDefaultProductCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigDefaultProductCommand.java @@ -26,7 +26,7 @@ public class ConfigDefaultProductCommand implements Callable { public Integer call() { Printer printer = global.printer(); if (!"aws".equals(product) && !"az".equals(product) && !"gcp".equals(product) && !"oci".equals(product)) { - printer.error("Unknown product '" + product + "'. Use: aws, az, gcp, oci"); + printer.error("Unknown product '" + product + "'.\nUse one of: aws, az, gcp, oci."); return 1; } GlobalConfigStore store = new GlobalConfigStore(); @@ -37,7 +37,8 @@ public class ConfigDefaultProductCommand implements Callable { printer.println(Ansi.gray("Config saved to: " + store.configFilePath())); return 0; } catch (IOException e) { - printer.error("Could not save config: " + e.getMessage()); + printer.error("Could not save config: " + e.getMessage(), + "Check that " + store.configFilePath().getParent() + " is writable and re-run the command."); return 1; } } diff --git a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java index 1c10d66..0e9a190 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java @@ -87,7 +87,7 @@ public class ConfigProfileCommand implements Callable { case "create" -> createProfile(printer, store); case "delete" -> deleteProfile(printer, store); default -> { - printer.error("Unknown action '" + action + "'. Use: list, show, create, delete"); + printer.error("Unknown action '" + action + "'.\nUse one of: list, show, create, delete."); yield 1; } }; @@ -138,16 +138,20 @@ public class ConfigProfileCommand implements Callable { } return 0; } catch (IOException e) { - printer.error("Could not list profiles: " + e.getMessage()); + printer.error("Could not list profiles: " + e.getMessage(), + "Check the files under ~/.floci/profiles and re-run the command."); return 1; } } private int showProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } + if (name == null) { printer.error(nameRequired()); return 1; } try { var profile = store.get(name); - if (profile.isEmpty()) { printer.error("Profile '" + name + "' not found."); return 1; } + if (profile.isEmpty()) { + printer.error("Profile '" + name + "' not found.\nRun '" + this.profile.commandPrefix() + " config profile list' to see available profiles."); + return 1; + } Profile p = profile.get(); ProfileDefaultValueProvider.warnAboutUnknownKeys(store, name, p); printer.println(Ansi.bold("Profile: ") + p.name); @@ -160,16 +164,16 @@ public class ConfigProfileCommand implements Callable { if (p.namespace != null) printer.println(" namespace: " + p.namespace); return 0; } catch (IOException e) { - printer.error("Could not read profile: " + e.getMessage()); + printer.error("Could not read profile: " + e.getMessage(), "Fix or delete the profile file, then re-run the command."); return 1; } } private int createProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } + if (name == null) { printer.error(nameRequired()); return 1; } try { if (store.get(name).isPresent()) { - printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.existingFile(name)); + printer.error("Profile '" + name + "' already exists.\nDelete it first, or edit " + store.existingFile(name) + "."); return 1; } if (port != null && (port < 1 || port > 65535)) { @@ -196,7 +200,7 @@ public class ConfigProfileCommand implements Callable { printer.println(Ansi.gray("Start it with: " + profile.commandPrefix() + " start --profile " + name)); return 0; } catch (IOException e) { - printer.error("Could not create profile: " + e.getMessage()); + printer.error("Could not create profile: " + e.getMessage(), "Check that ~/.floci/profiles is writable and re-run the command."); return 1; } } @@ -255,7 +259,7 @@ public class ConfigProfileCommand implements Callable { } private int deleteProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } + if (name == null) { printer.error(nameRequired()); return 1; } try { List removed = store.delete(name); if (removed.isEmpty()) { @@ -268,8 +272,12 @@ public class ConfigProfileCommand implements Callable { printer.println(Ansi.green("Deleted") + " profile '" + name + "'" + files); return 0; } catch (IOException e) { - printer.error("Could not delete profile: " + e.getMessage()); + printer.error("Could not delete profile: " + e.getMessage(), "Check that ~/.floci/profiles is writable and re-run the command."); return 1; } } + + private String nameRequired() { + return "Profile name required.\nPass it after the action, for example '" + profile.commandPrefix() + " config profile " + action + " dev'."; + } } diff --git a/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java index b672df7..b8c2915 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java @@ -48,7 +48,7 @@ public class ConfigValidateCommand implements Callable { Path composeFile = resolveComposeFile(); if (composeFile == null) { - printer.error("No docker-compose file found. Pass --file or run in a directory with docker-compose.yml."); + printer.error("No docker-compose file found.\nPass --file , or run in a directory with docker-compose.yml."); return 1; } @@ -56,7 +56,8 @@ public class ConfigValidateCommand implements Callable { try { content = Files.readString(composeFile); } catch (IOException e) { - printer.error("Could not read " + composeFile + ": " + e.getMessage()); + printer.error("Could not read " + composeFile + ": " + e.getMessage(), + "Check the file's permissions, or pass another with --file."); return 1; } 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 484581c..51def60 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java @@ -51,7 +51,8 @@ public class SnapshotDeleteCommand implements Callable { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { - printer.error("Failed to delete snapshot: " + e.getMessage()); + printer.error("Failed to delete snapshot: " + e.getMessage(), + "Run '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator."); } return 1; } 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 4ae84af..b8b493f 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java @@ -58,7 +58,8 @@ public class SnapshotListCommand implements Callable { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { - printer.error("Failed to list snapshots: " + e.getMessage()); + printer.error("Failed to list snapshots: " + e.getMessage(), + "Run '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator."); } return 1; } 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 5436c78..93c8547 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java @@ -51,7 +51,8 @@ public class SnapshotLoadCommand implements Callable { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { - printer.error("Failed to load snapshot: " + e.getMessage()); + printer.error("Failed to load snapshot: " + e.getMessage(), + "Run '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator."); } return 1; } 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 1badd36..4b19c40 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java @@ -58,7 +58,8 @@ public class SnapshotSaveCommand implements Callable { printer.error("Snapshot API not available on this server version.\n" + "Track progress: " + profile.serverRepo() + "/issues"); } else { - printer.error("Failed to save snapshot: " + e.getMessage()); + printer.error("Failed to save snapshot: " + e.getMessage(), + "Run '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator."); } return 1; } diff --git a/src/main/java/io/floci/cli/docker/DockerClient.java b/src/main/java/io/floci/cli/docker/DockerClient.java index ad9ff2f..b07b146 100644 --- a/src/main/java/io/floci/cli/docker/DockerClient.java +++ b/src/main/java/io/floci/cli/docker/DockerClient.java @@ -27,6 +27,9 @@ public class DockerClient { /** How long a non-streaming docker call may take before it is killed. */ public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30); + /** The next step for a failed docker call. */ + public static final String CHECK_DOCKER = "Check that Docker is running ('docker info') and re-run the command."; + private final String binary; private final Duration timeout; @@ -252,7 +255,7 @@ public class DockerClient { String out = stdout.join(); String err = stderr.join(); if (proc.exitValue() != 0) { - throw new DockerException(err.isBlank() ? out : err); + throw DockerException.ofOutput(err.isBlank() ? out : err); } return out; } catch (InterruptedException e) { diff --git a/src/main/java/io/floci/cli/docker/DockerException.java b/src/main/java/io/floci/cli/docker/DockerException.java index 4a0bf6f..d84ca8e 100644 --- a/src/main/java/io/floci/cli/docker/DockerException.java +++ b/src/main/java/io/floci/cli/docker/DockerException.java @@ -1,7 +1,30 @@ package io.floci.cli.docker; public class DockerException extends Exception { + + private final boolean advised; + + /** An error in this CLI's own words; a second line in it is its next step. */ public DockerException(String message) { + this(message, message != null && message.strip().contains("\n")); + } + + private DockerException(String message, boolean advised) { super(message); + this.advised = advised; + } + + /** + * An error that is docker's own output. However many lines it has, none of them is advice + * from this CLI, so it never counts as already saying what to do next. + */ + public static DockerException ofOutput(String output) { + return new DockerException(output == null ? "" : output.strip(), false); + } + + /** The message followed by {@code nextStep}, unless the message already ends with its own. */ + public String withNextStep(String nextStep) { + String message = getMessage() == null ? "" : getMessage().strip(); + return advised ? message : message + "\n" + nextStep; } } diff --git a/src/main/java/io/floci/cli/output/Printer.java b/src/main/java/io/floci/cli/output/Printer.java index f403a1c..6945b83 100644 --- a/src/main/java/io/floci/cli/output/Printer.java +++ b/src/main/java/io/floci/cli/output/Printer.java @@ -41,6 +41,16 @@ public final class Printer { err.println(Ansi.red("Error: ") + message); } + /** + * Reports {@code message} followed by {@code nextStep}, unless the message already carries its + * own (a second line), as messages from DockerException or FlociException often do. + */ + public void error(String message, String nextStep) { + // A trailing newline is not a second line: forwarded output often ends with one. + String text = message.stripTrailing(); + error(text.contains("\n") ? text : text + "\n" + nextStep); + } + public void warn(String message) { err.println(Ansi.yellow("Warning: ") + message); } diff --git a/src/test/java/io/floci/cli/unit/ErrorHintConventionTest.java b/src/test/java/io/floci/cli/unit/ErrorHintConventionTest.java new file mode 100644 index 0000000..3a7ec4f --- /dev/null +++ b/src/test/java/io/floci/cli/unit/ErrorHintConventionTest.java @@ -0,0 +1,73 @@ +package io.floci.cli.unit; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * AGENTS.md: every {@code printer.error(...)} ends with a suggested next step. A call that writes + * its own text must either put a second line in it or pass the next step as a second argument; + * a call that only forwards a message (a variable or {@code e.getMessage()}) is trusted to forward + * one that already has it. {@code e.withNextStep(...)} on a DockerException is the third way: the + * exception knows whether its text is docker's raw output or already carries advice. + */ +class ErrorHintConventionTest { + + @Test + void everyErrorWithItsOwnTextEndsWithANextStep() throws Exception { + List missing = new ArrayList<>(); + try (Stream files = Files.walk(Path.of("src/main/java"))) { + for (Path file : files.filter(p -> p.toString().endsWith(".java")).toList()) { + String src = Files.readString(file); + int at = 0; + while ((at = src.indexOf("printer.error(", at)) >= 0) { + int start = at + "printer.error(".length(); + String args = argumentsFrom(src, start); + if (args.contains("\"") && !args.contains("\\n") && !hasTopLevelComma(args) + && !args.contains(".withNextStep(")) { + int line = (int) src.substring(0, at).chars().filter(c -> c == '\n').count() + 1; + missing.add(file + ":" + line); + } + at = start; + } + } + } + assertEquals(List.of(), missing, "error messages without a next step"); + } + + // The text between the opening parenthesis at 'start' and its matching close, string-aware. + private static String argumentsFrom(String src, int start) { + int depth = 1; + int i = start; + while (depth > 0) { + char c = src.charAt(i); + if (c == '"') { + i++; + while (src.charAt(i) != '"') i += src.charAt(i) == '\\' ? 2 : 1; + } else if (c == '(') { + depth++; + } else if (c == ')') { + depth--; + } + i++; + } + return src.substring(start, i - 1); + } + + private static boolean hasTopLevelComma(String args) { + String noStrings = args.replaceAll("\"(\\\\.|[^\"\\\\])*\"", "\"\""); + int depth = 0; + for (char c : noStrings.toCharArray()) { + if (c == '(') depth++; + else if (c == ')') depth--; + else if (c == ',' && depth == 0) return true; + } + return false; + } +} diff --git a/src/test/java/io/floci/cli/unit/PrinterErrorTest.java b/src/test/java/io/floci/cli/unit/PrinterErrorTest.java new file mode 100644 index 0000000..ab71916 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/PrinterErrorTest.java @@ -0,0 +1,82 @@ +package io.floci.cli.unit; + +import io.floci.cli.docker.DockerClient; +import io.floci.cli.docker.DockerException; +import io.floci.cli.output.Ansi; +import io.floci.cli.output.OutputFormat; +import io.floci.cli.output.Printer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.*; + +class PrinterErrorTest { + + private static String errorOf(String message, String nextStep) { + Ansi.disable(); + ByteArrayOutputStream err = new ByteArrayOutputStream(); + new Printer(new PrintStream(new ByteArrayOutputStream()), new PrintStream(err), OutputFormat.text, false) + .error(message, nextStep); + return err.toString().strip(); + } + + @Test + void theNextStepFollowsAOneLineMessage() { + assertEquals("Error: Failed to stop container: boom\nCheck Docker.", + errorOf("Failed to stop container: boom", "Check Docker.")); + } + + /** A message that already brings its own next step (a second line) is not given another. */ + @Test + void aMessageWithItsOwnNextStepIsLeftAlone() { + assertEquals("Error: Timed out.\nRe-run it.", errorOf("Timed out.\nRe-run it.", "Check Docker.")); + } + + /** A trailing newline is not a second line, so the next step still follows. */ + @Test + void aTrailingNewlineDoesNotHideTheNextStep() { + assertEquals("Error: Failed to stop container: boom\nCheck Docker.", + errorOf("Failed to stop container: boom\n", "Check Docker.")); + } + + /** Docker's own output never counts as advice, whether it ends in a newline or spans several lines. */ + @Test + void dockersOwnOutputAlwaysGetsTheNextStep() { + assertEquals("Error response from daemon: boom\nCheck Docker.", + DockerException.ofOutput("Error response from daemon: boom\n").withNextStep("Check Docker.")); + assertEquals("Error response from daemon: boom\nSee 'docker run --help'.\nCheck Docker.", + DockerException.ofOutput("Error response from daemon: boom\nSee 'docker run --help'.\n") + .withNextStep("Check Docker.")); + } + + /** An error in the CLI's own words that already says what to do is not given a second next step. */ + @Test + void anErrorWithItsOwnAdviceKeepsIt() { + assertEquals("'docker stop' did not finish within 30s.\nCheck that the Docker daemon is responsive.", + new DockerException("'docker stop' did not finish within 30s.\nCheck that the Docker daemon is responsive.") + .withNextStep("Check Docker.")); + assertEquals("Interrupted\nCheck Docker.", new DockerException("Interrupted").withNextStep("Check Docker.")); + } + + /** Through the real docker call: stderr ending in a newline reaches the user with the next step. */ + @Test + @DisabledOnOs(OS.WINDOWS) + void aFailedDockerCallCarriesTheNextStep(@TempDir Path dir) throws Exception { + Path fakeDocker = dir.resolve("docker"); + Files.writeString(fakeDocker, "#!/bin/sh\necho 'Error response from daemon: cannot stop container' >&2\nexit 1\n"); + assertTrue(fakeDocker.toFile().setExecutable(true)); + + DockerException e = assertThrows(DockerException.class, + () -> new DockerClient(fakeDocker.toString()).stopContainer("floci", 1)); + + assertEquals("Error response from daemon: cannot stop container\n" + DockerClient.CHECK_DOCKER, + e.withNextStep(DockerClient.CHECK_DOCKER)); + } +} diff --git a/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java b/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java index 1bd3f05..e8190c0 100644 --- a/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java +++ b/src/test/java/io/floci/cli/unit/SnapshotCommandsTest.java @@ -103,4 +103,16 @@ class SnapshotCommandsTest { assertTrue(r[2].contains("Snapshot API not available"), r[2]); } + + /** The hint has to check the instance that failed, not the default one. */ + @ParameterizedTest + @CsvSource({"list,", "delete, nightly", "save, nightly", "load, nightly"}) + void aFailedSnapshotCallPointsAtTheSameInstance(String command, String name) { + status = 500; + + String[] r = name == null ? run("snapshot", command) : run("snapshot", command, name); + + assertEquals("1", r[0]); + assertTrue(r[2].contains("floci status --container floci-snapshot-test-none"), r[2]); + } }