From b2baba3e6c1912356cc639c25c7d2c59683b0865 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Fri, 9 Oct 2026 00:21:04 -0500 Subject: [PATCH] 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 --- .../io/floci/cli/commands/LogsCommand.java | 4 +- .../io/floci/cli/commands/ResetCommand.java | 5 +- .../io/floci/cli/commands/StartCommand.java | 6 +- .../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 | 3 + .../java/io/floci/cli/output/Printer.java | 8 +++ .../cli/unit/ErrorHintConventionTest.java | 71 +++++++++++++++++++ .../io/floci/cli/unit/PrinterErrorTest.java | 34 +++++++++ 16 files changed, 161 insertions(+), 30 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..1cd63c4 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.getMessage(), 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.getMessage(), 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..d3f8cba 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -331,7 +331,8 @@ 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. + printer.error("Failed to inspect container: " + e.getMessage() + socketGuidance(daemon), DockerClient.CHECK_DOCKER); return 1; } @@ -360,7 +361,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..ad765d9 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.getMessage(), 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.getMessage(), 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.getMessage(), 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..566cd0e 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' 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..adf4d9a 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' 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..3f68832 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' 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..12e3391 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' 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..c1f8740 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; diff --git a/src/main/java/io/floci/cli/output/Printer.java b/src/main/java/io/floci/cli/output/Printer.java index f403a1c..901835a 100644 --- a/src/main/java/io/floci/cli/output/Printer.java +++ b/src/main/java/io/floci/cli/output/Printer.java @@ -41,6 +41,14 @@ 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) { + error(message.contains("\n") ? message : message + "\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..1ca89f1 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/ErrorHintConventionTest.java @@ -0,0 +1,71 @@ +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. + */ +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)) { + 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..4b1680c --- /dev/null +++ b/src/test/java/io/floci/cli/unit/PrinterErrorTest.java @@ -0,0 +1,34 @@ +package io.floci.cli.unit; + +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 java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +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.")); + } +}