Treat IO and parse failures as first-class outcomes, with behavior that is (a) deterministic, (b) contract-respecting, and (c) strict about “malformed but present” config.
Coding standard
try/except around file reads and decoding/parsing (e.g., read_text, json.loads, tomllib.load) and do not let tracebacks leak to users/CI.error: ... to stderr and either:
Example pattern
from pathlib import Path
import json, sys
def load_required_json(path: Path) -> dict:
try:
data = path.read_text(encoding="utf-8")
return json.loads(data)
except FileNotFoundError:
sys.stderr.write(f"error: findings file not found: {path}
")
raise SystemExit(1)
except json.JSONDecodeError:
sys.stderr.write(f"error: findings file is not valid JSON: {path}
")
raise SystemExit(1)
# Optional vs required distinction
def load_optional_toml(path: Path) -> dict:
if not path.exists():
return {} # missing is OK
try:
# parse toml here
...
except Exception:
sys.stderr.write(f"error: customization exists but failed to parse: {path}
")
raise SystemExit(1) # malformed-but-present is not silently ignored
Applying this consistently prevents hidden misconfigurations, keeps tooling predictable, and matches each script’s stated integration contract.