diff --git a/graphify/affected.py b/graphify/affected.py index bd70b276..dbb532b4 100644 --- a/graphify/affected.py +++ b/graphify/affected.py @@ -210,7 +210,13 @@ def load_graph(path: Path) -> nx.Graph: import json from networkx.readwrite import json_graph - raw = json.loads(path.read_text(encoding="utf-8")) + try: + raw = json.loads(path.read_text(encoding="utf-8")) + except (json.JSONDecodeError, OSError) as exc: + raise RuntimeError( + f"Cannot read graph file {path}: {exc}. " + "Re-run 'graphify extract' to regenerate it." + ) from exc # Force directed so stored caller→callee direction survives the round-trip; # mirrors serve.py and __main__.py (#1174). raw = {**raw, "directed": True} diff --git a/graphify/build.py b/graphify/build.py index 2de02b2a..6fcf241b 100644 --- a/graphify/build.py +++ b/graphify/build.py @@ -746,7 +746,13 @@ def build_merge( # NetworkX round-trip loses direction permanently (#760). from graphify.security import check_graph_file_size_cap check_graph_file_size_cap(graph_path) - data = json.loads(graph_path.read_text(encoding="utf-8")) + try: + data = json.loads(graph_path.read_text(encoding="utf-8")) + except (json.JSONDecodeError, OSError) as exc: + raise RuntimeError( + f"Cannot read {graph_path} for incremental merge: {exc}. " + "Delete the file and run a full rebuild." + ) from exc links_key = "links" if "links" in data else "edges" existing_nodes = list(data.get("nodes", [])) existing_edges = list(data.get(links_key, [])) diff --git a/graphify/diagnostics.py b/graphify/diagnostics.py index 4d8abe29..ff66fa95 100644 --- a/graphify/diagnostics.py +++ b/graphify/diagnostics.py @@ -274,7 +274,13 @@ def _read_json_file(path: str | Path) -> dict[str, Any]: json_path = Path(path) check_graph_file_size_cap(json_path) - data = json.loads(json_path.read_text(encoding="utf-8")) + try: + data = json.loads(json_path.read_text(encoding="utf-8")) + except (json.JSONDecodeError, OSError) as exc: + raise RuntimeError( + f"Cannot parse {json_path}: {exc}. " + "The file may be corrupted — re-run 'graphify extract'." + ) from exc if not isinstance(data, dict): raise ValueError("diagnostic input must be a JSON object") return data