Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions graphify/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def _load_graph(graph_path: str) -> nx.Graph:
except Exception:
G.graph["_learning_overlay"] = {}
return G
except (ValueError, FileNotFoundError) as exc:
print(f"error: {exc}", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError as exc:
print(f"error: graph.json is corrupted ({exc}). Re-run /graphify to rebuild.", file=sys.stderr)
sys.exit(1)
except (ValueError, FileNotFoundError) as exc:
print(f"error: {exc}", file=sys.stderr)
sys.exit(1)


def _communities_from_graph(G: nx.Graph) -> dict[int, list[str]]:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,32 @@ def test_load_graph_missing_file(tmp_path):
_load_graph(str(graphify_dir / "nonexistent.json"))


def test_load_graph_corrupted_json_prints_recovery_message(tmp_path, capsys):
"""json.JSONDecodeError is a ValueError subclass, so its except clause
must be checked before the bare (ValueError, FileNotFoundError) clause,
or the corrupted-graph recovery hint is unreachable (#2005)."""
p = tmp_path / "graph.json"
p.write_text("{not valid json")
with pytest.raises(SystemExit):
_load_graph(str(p))
err = capsys.readouterr().err
assert "graph.json is corrupted" in err
assert "Re-run /graphify to rebuild" in err


def test_load_graph_generic_value_error_message_unchanged(tmp_path, capsys):
"""A non-decode ValueError (e.g. a non-.json path) must still print the
generic error, not the corrupted-graph hint — pins the except-clause
order from #2005 so a future refactor can't collapse them back."""
p = tmp_path / "graph.txt"
p.write_text("not a graph")
with pytest.raises(SystemExit):
_load_graph(str(p))
err = capsys.readouterr().err
assert "must be a .json file" in err
assert "corrupted" not in err


def test_load_graph_rejects_oversized_file(monkeypatch, tmp_path, capsys):
# #F4: oversized graph.json must fail fast (SystemExit) with a clear error.
G = _make_graph()
Expand Down