Skip to content

Commit df89a23

Browse files
[FIX] Guard non-dict error items in 404 enrichment (CodeRabbit)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X8t7DFHq7Dj655kEywKk3K
1 parent 7e43f35 commit df89a23

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

backend/middleware/exception.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def _enrich_not_found_detail(response: Response | None, context: Any) -> None:
7373
return
7474
label = str(model._meta.verbose_name).capitalize()
7575
for err in data.get("errors", []):
76-
if err.get("code") == "not_found":
76+
# Guard: a raise here would mask the original error with a 500.
77+
if isinstance(err, dict) and err.get("code") == "not_found":
7778
err["detail"] = f"{label} not found."
7879

7980

backend/middleware/test_exception.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ def test_non_404_untouched():
5757

5858
def test_none_response_is_safe():
5959
_enrich_not_found_detail(None, {})
60+
61+
62+
def test_non_dict_error_item_does_not_raise():
63+
resp = _Resp(
64+
404, {"errors": ["unexpected", {"code": "not_found", "detail": "Not found."}]}
65+
)
66+
_enrich_not_found_detail(resp, {"view": _View()})
67+
assert resp.data["errors"][1]["detail"] == "Lookup definition not found."

0 commit comments

Comments
 (0)