Skip to content

Commit 11907c9

Browse files
authored
fix(cli): catch JSONDecodeError, exit 2 with clean message
Adds JSONDecodeError handler in `_cmd_verify` so a structurally invalid log surfaces `structurally invalid log: …` on stderr and exits 2, instead of leaking a Python traceback. Locks the UX contract with a subprocess-level test that asserts 'Traceback' is absent from stderr. Closes #15. Same bug was also caught in #14 (closed as duplicate). Co-authored-by: akmhatey-ai <akmhatey-ai@users.noreply.github.com>
1 parent 8262337 commit 11907c9

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

falsify_inspect/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def _cmd_verify(args: argparse.Namespace) -> int:
4343
except PRMLVerificationError as exc:
4444
sys.stderr.write(f"verification error: {exc}\n")
4545
return 3
46+
except json.JSONDecodeError as exc:
47+
sys.stderr.write(f"structurally invalid log: {exc}\n")
48+
return 2
4649
except FileNotFoundError as exc:
4750
sys.stderr.write(f"log not found: {exc}\n")
4851
return 2

tests/test_cli.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Tests for falsify_inspect.cli."""
2+
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
8+
def test_verify_malformed_json_exits_2_without_traceback(tmp_path: Path):
9+
log_path = tmp_path / "broken.log"
10+
log_path.write_text("not valid json", encoding="utf-8")
11+
12+
result = subprocess.run(
13+
[
14+
sys.executable,
15+
"-m",
16+
"falsify_inspect.cli",
17+
"verify",
18+
str(log_path),
19+
"--hash",
20+
"deadbeef",
21+
"--threshold",
22+
"0.5",
23+
"--threshold-direction",
24+
">=",
25+
"--pre-registered",
26+
"2026-01-01T00:00:00Z",
27+
],
28+
capture_output=True,
29+
text=True,
30+
check=False,
31+
)
32+
33+
assert result.returncode == 2
34+
assert "structurally invalid log:" in result.stderr
35+
assert "Traceback" not in result.stderr

0 commit comments

Comments
 (0)