mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
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 <hectorvent@gmail.com> * 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 <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
6a74d7d2d5
commit
bd4fa0f134
18 files changed
+252
-31
No files matched your search
@@ -54,7 +54,7 @@ public class LogsCommand implements Callable<Integer> {
|
||||
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<Integer> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,10 +152,9 @@ public class ResetCommand implements Callable<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -331,7 +331,11 @@ public class StartCommand implements Callable<Integer> {
|
||||
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<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ public class StopCommand implements Callable<Integer> {
|
||||
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<Integer> {
|
||||
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<Integer> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,10 +142,10 @@ public class UpdateCommand implements Callable<Integer> {
|
||||
+ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ConfigDefaultProductCommand implements Callable<Integer> {
|
||||
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<Integer> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
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<Integer> {
|
||||
}
|
||||
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<Integer> {
|
||||
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<Integer> {
|
||||
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<Integer> {
|
||||
}
|
||||
|
||||
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<Path> removed = store.delete(name);
|
||||
if (removed.isEmpty()) {
|
||||
@@ -268,8 +272,12 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
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'.";
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ public class ConfigValidateCommand implements Callable<Integer> {
|
||||
|
||||
Path composeFile = resolveComposeFile();
|
||||
if (composeFile == null) {
|
||||
printer.error("No docker-compose file found. Pass --file <path> or run in a directory with docker-compose.yml.");
|
||||
printer.error("No docker-compose file found.\nPass --file <path>, or run in a directory with docker-compose.yml.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,8 @@ public class ConfigValidateCommand implements Callable<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,8 @@ public class SnapshotDeleteCommand implements Callable<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,8 @@ public class SnapshotListCommand implements Callable<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,8 @@ public class SnapshotLoadCommand implements Callable<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,8 @@ public class SnapshotSaveCommand implements Callable<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user