fix(cli): emit JSON error on --output-format json — ROADMAP #42

When claw --output-format json hits an error, the error was previously
printed as plain prose to stderr, making it invisible to downstream tooling
that parses JSON output. Now:

  {"type":"error","error":"api returned 401 ..."}

Detection: scan argv at process exit for --output-format json or
--output-format=json. Non-JSON error path unchanged. 156 CLI tests pass.
This commit is contained in:
YeonGyu-Kim
2026-04-09 16:33:20 +09:00
parent 7d9f11b91f
commit 1a8f73da01
2 changed files with 18 additions and 1 deletions

View File

@@ -110,7 +110,22 @@ type RuntimePluginStateBuildOutput = (
fn main() {
if let Err(error) = run() {
let message = error.to_string();
if message.contains("`claw --help`") {
// When --output-format json is active, emit errors as JSON so downstream
// tools can parse failures the same way they parse successes (ROADMAP #42).
let argv: Vec<String> = std::env::args().collect();
let json_output = argv
.windows(2)
.any(|w| w[0] == "--output-format" && w[1] == "json")
|| argv.iter().any(|a| a == "--output-format=json");
if json_output {
eprintln!(
"{}",
serde_json::json!({
"type": "error",
"error": message,
})
);
} else if message.contains("`claw --help`") {
eprintln!("error: {message}");
} else {
eprintln!(