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>
This commit is contained in:
Hector Ventura committed 2026-10-10 22:31:49 -05:00
1 parent 6a74d7d2d5
commit b2baba3e6c
16 files changed
+161 -30

No files matched your search

@@ -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<String> missing = new ArrayList<>();
try (Stream<Path> 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;
}
}