mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 06:00:47 -05:00
fix(cli): keep the next step when an error's cause has several lines (#67)
A failed docker run with more than one line of output lost its 'floci doctor' hint, and a profile or Compose file that does not parse was reported with the parser's source-location line instead of what to do. Causes are now shown as one line, and docker's own output always gets its next step. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
68d28d49ef
commit
188aa23edc
13 files changed
+149
-28
No files matched your search
@@ -377,8 +377,10 @@ public class StartCommand implements Callable<Integer> {
|
||||
id = docker.startContainer(args);
|
||||
progress.accept(Ansi.green("Container started") + " (" + id.substring(0, Math.min(12, id.length())) + ")");
|
||||
} catch (DockerException e) {
|
||||
printer.error("Failed to start container: " + e.getMessage(),
|
||||
"Run '" + profile.commandPrefix() + " doctor' to check Docker, the image and the port.");
|
||||
// withNextStep, not the two-argument error: docker's own output is often several
|
||||
// lines, which must not be taken for a next step.
|
||||
printer.error("Failed to start container: " + e.withNextStep(
|
||||
"Run '" + profile.commandPrefix() + " doctor' to check Docker, the image and the port."));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.config.GlobalConfigStore;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.Printer;
|
||||
import io.floci.cli.util.Causes;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -37,7 +38,7 @@ 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: " + Causes.firstLine(e),
|
||||
"Check that " + store.configFilePath().getParent() + " is writable and re-run the command.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.floci.cli.config.ProfileStore;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
import io.floci.cli.util.Causes;
|
||||
import picocli.CommandLine.*;
|
||||
import picocli.CommandLine.Model.CommandSpec;
|
||||
|
||||
@@ -149,7 +150,7 @@ 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: " + Causes.firstLine(e),
|
||||
"Check the files under ~/.floci/profiles and re-run the command.");
|
||||
return 1;
|
||||
}
|
||||
@@ -175,7 +176,7 @@ 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(), "Fix or delete the profile file, then re-run the command.");
|
||||
printer.error("Could not read profile: " + Causes.firstLine(e), "Fix or delete the profile file, then re-run the command.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -212,7 +213,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
printer.error("Interrupted while " + editor.get(0) + " was open.\nRe-run '" + prefix + "edit " + name + "'.");
|
||||
return 1;
|
||||
} catch (IOException e) {
|
||||
printer.error("Could not start the editor '" + String.join(" ", editor) + "': " + e.getMessage(),
|
||||
printer.error("Could not start the editor '" + String.join(" ", editor) + "': " + Causes.firstLine(e),
|
||||
"Set EDITOR to a command on your PATH, or edit " + file + " yourself.");
|
||||
return 1;
|
||||
}
|
||||
@@ -231,8 +232,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
}
|
||||
ProfileDefaultValueProvider.warnAboutUnknownKeys(store, name, edited.get());
|
||||
} catch (IOException e) {
|
||||
String reason = e.getMessage() == null ? "unreadable" : e.getMessage().lines().findFirst().orElse("unreadable");
|
||||
printer.error("Profile '" + name + "' is no longer valid: " + reason + "\n"
|
||||
printer.error("Profile '" + name + "' is no longer valid: " + Causes.firstLine(e) + "\n"
|
||||
+ "Re-run '" + prefix + "edit " + name + "' to fix it.");
|
||||
return 1;
|
||||
}
|
||||
@@ -329,7 +329,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(), "Check that ~/.floci/profiles is writable and re-run the command.");
|
||||
printer.error("Could not create profile: " + Causes.firstLine(e), "Check that ~/.floci/profiles is writable and re-run the command.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -401,7 +401,7 @@ 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(), "Check that ~/.floci/profiles is writable and re-run the command.");
|
||||
printer.error("Could not delete profile: " + Causes.firstLine(e), "Check that ~/.floci/profiles is writable and re-run the command.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.Glyphs;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
import io.floci.cli.util.Causes;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -59,7 +60,7 @@ 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 + ": " + Causes.firstLine(e),
|
||||
"Check the file's permissions, or pass another with --file.");
|
||||
return 1;
|
||||
}
|
||||
@@ -72,8 +73,7 @@ public class ConfigValidateCommand implements Callable<Integer> {
|
||||
unreadable = "Could not read " + composeFile + " as a Compose file: it has no top-level mapping";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
String reason = e.getMessage() == null ? "unreadable" : e.getMessage().lines().findFirst().orElse("unreadable");
|
||||
unreadable = "Could not read " + composeFile + " as YAML: " + reason;
|
||||
unreadable = "Could not read " + composeFile + " as YAML: " + Causes.firstLine(e);
|
||||
}
|
||||
if (unreadable != null) {
|
||||
// Structured output always carries the report, so a script reading it gets 'valid: false'
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.cli.util.Causes;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
@@ -85,8 +86,7 @@ public final class YamlTree {
|
||||
Iterator<Object> documents = reader.loadAll(yaml).iterator();
|
||||
first = documents.hasNext() ? documents.next() : null;
|
||||
} catch (YAMLException | IllegalArgumentException e) {
|
||||
String reason = e.getMessage() == null ? "not valid YAML" : e.getMessage().lines().findFirst().orElse("not valid YAML");
|
||||
throw new IOException(reason);
|
||||
throw new IOException(Causes.firstLine(e));
|
||||
}
|
||||
return first == null ? NODES.missingNode() : tree(first, new int[] {MAX_NODES}, 0);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.util.Causes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
@@ -49,10 +50,7 @@ public class GlobalConfigStore {
|
||||
|
||||
private static String reason(IOException e) {
|
||||
if (e instanceof AccessDeniedException) return "permission denied";
|
||||
String message = e.getMessage();
|
||||
if (message == null) return e.getClass().getSimpleName();
|
||||
int newline = message.indexOf('\n');
|
||||
return newline >= 0 ? message.substring(0, newline) : message;
|
||||
return Causes.firstLine(e);
|
||||
}
|
||||
|
||||
public void setDefaultProduct(String product) throws IOException {
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.floci.cli.config;
|
||||
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.util.Causes;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.IDefaultValueProvider;
|
||||
import picocli.CommandLine.Model.ArgSpec;
|
||||
@@ -117,7 +118,7 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ProfileNotFoundException(commandLine, e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw invalid(commandLine, name, e.getMessage());
|
||||
throw invalid(commandLine, name, Causes.firstLine(e));
|
||||
}
|
||||
if (loaded.isEmpty()) {
|
||||
throw new ProfileNotFoundException(commandLine,
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.floci.cli.config;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.util.Causes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -75,7 +76,7 @@ public class ProfileStore {
|
||||
// The cause is quoted, not guessed: this is a parse error or a
|
||||
// file that could not be opened.
|
||||
System.err.println(Ansi.yellow("Warning: ") + "Could not read profile file " + p
|
||||
+ ": " + firstLine(e.getMessage())
|
||||
+ ": " + Causes.firstLine(e)
|
||||
+ "\nCheck the file and its permissions, or delete it.");
|
||||
}
|
||||
});
|
||||
@@ -89,12 +90,6 @@ public class ProfileStore {
|
||||
return Optional.of(read(file));
|
||||
}
|
||||
|
||||
private static String firstLine(String message) {
|
||||
if (message == null) return "unreadable";
|
||||
int newline = message.indexOf('\n');
|
||||
return newline >= 0 ? message.substring(0, newline) : message;
|
||||
}
|
||||
|
||||
// The one read path, so list and show agree on a profile's name.
|
||||
private static Profile read(Path file) throws IOException {
|
||||
Profile profile = YAML.readValue(file.toFile(), Profile.class);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.floci.cli.util;
|
||||
|
||||
/**
|
||||
* Turns an exception into the one line a user is shown. Parser errors from Jackson and YAML come
|
||||
* with a second line of source location ("at [Source: ...; line: 3]"), which is noise in a
|
||||
* message and, worse, looks like a next step to {@code Printer.error(message, nextStep)}.
|
||||
*/
|
||||
public final class Causes {
|
||||
|
||||
private Causes() {}
|
||||
|
||||
/** The first line of {@code e}'s message, or its type name when it has none. */
|
||||
public static String firstLine(Throwable e) {
|
||||
String message = e.getMessage();
|
||||
if (message == null || message.isBlank()) return e.getClass().getSimpleName();
|
||||
return firstLine(message);
|
||||
}
|
||||
|
||||
/** The first non-empty line of {@code message}. */
|
||||
public static String firstLine(String message) {
|
||||
return message.strip().lines().findFirst().orElse(message).strip();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user