Read JSON with a useful error message.
(path: str | Path, default=None)
| 92 | # ────────────────────────────────────────────── |
| 93 | |
| 94 | def read_json(path: str | Path, default=None): |
| 95 | """Read JSON with a useful error message.""" |
| 96 | if not path: |
| 97 | return default |
| 98 | path = Path(path) |
| 99 | if not path.exists(): |
| 100 | return default |
| 101 | try: |
| 102 | return json.loads(path.read_text(encoding="utf-8")) |
| 103 | except json.JSONDecodeError as exc: |
| 104 | raise SystemExit(f"ERROR: invalid JSON in {path}: {exc}") from exc |
| 105 | |
| 106 | |
| 107 | def first_present(mapping: dict, *keys, default=None): |
no outgoing calls
no test coverage detected