[Bug]: Malformed or invalid compare JSON causes unhandled exceptions
Summary
When --compare points to a file that exists but contains invalid JSON or a schema incompatible with TracerData.from_dict(), OracleTrace raises an unhandled exception instead of exiting with a clear error message.
Expected Behavior
Invalid compare files should produce a descriptive stderr message and exit code 1, similar to other CLI validation errors (e.g. missing target, invalid regex, compare file not found).
Actual Behavior
_compare_and_fail() loads and parses the compare file without error handling:
with open(args.compare, "r", encoding="utf-8") as f:
old_data: TracerData = TracerData.from_dict(json.load(f))
Malformed JSON → json.JSONDecodeError traceback. Missing required keys → KeyError or TypeError traceback.
Steps to Reproduce
-
Run command:
-
Use input/script:
-
Observe output/error:
-
Create
baseline.jsonwith invalid content:{invalid json -
Run
oracletrace app.py --compare baseline.json -
Observe an unhandled Python traceback
Repeat with valid JSON but wrong schema (e.g. {"foo": "bar"}).
Command and Output
echo '{invalid json' > baseline.json
oracletrace app.py --compare baseline.json
json.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Environment
- OracleTrace version: 3.0.0
- Python version: 3.11+
- OS: any
- Shell/terminal (if relevant): any
Additional Context
Suggested fix in oracletrace/cli.py _compare_and_fail():
- Wrap
json.load()intry/except json.JSONDecodeError - Wrap
TracerData.from_dict()intry/except (KeyError, TypeError, ValueError) - Print user-friendly messages to stderr and return exit code
1
Add tests in tests/test_cli.py for malformed JSON and invalid schema files.
Note: missing compare file is already handled (returns 1 with "Compare file not found:").
Checklist
- [x] I searched existing issues before opening this report
- [x] I provided a reproducible example or clear reproduction steps
- [x] I included version and environment details