oracletrace icon indicating copy to clipboard operation
oracletrace copied to clipboard

[Bug]: Malformed or invalid compare JSON causes unhandled exceptions

Open KaykCaputo opened this issue 5 months ago • 0 comments

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

  1. Run command:

  2. Use input/script:

  3. Observe output/error:

  4. Create baseline.json with invalid content: {invalid json

  5. Run oracletrace app.py --compare baseline.json

  6. 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() in try/except json.JSONDecodeError
  • Wrap TracerData.from_dict() in try/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

KaykCaputo avatar Jun 16 '26 18:06 KaykCaputo