-
Notifications
You must be signed in to change notification settings - Fork 632
[FIX] Surface non_field_errors and name resources in 404s in the central DRF error path #2099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bde1a5a
[FIX] Surface non_field_errors as a toast instead of failing silently
chandrasekharan-zipstack fba80b7
[FIX] Address review: guard non-array errors, use bullet separator
chandrasekharan-zipstack 1b654ed
[MISC] Tighten non-field-error comments to concise WHY-only
chandrasekharan-zipstack 8bfb2d3
Merge remote-tracking branch 'origin/main' into fix/fe-non-field-errors
chandrasekharan-zipstack e6613ad
[FIX] Name the resource in DRF 404s via verbose_name
chandrasekharan-zipstack 7e43f35
[FIX] Drop DEBUG=True from test settings (SonarCloud S4507)
chandrasekharan-zipstack df89a23
[FIX] Guard non-dict error items in 404 enrichment (CodeRabbit)
chandrasekharan-zipstack e8d3477
Merge branch 'main' into fix/fe-non-field-errors
chandrasekharan-zipstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Unit checks for the 404 detail enrichment in the DRF exception handler. | ||
|
|
||
| Pure-logic; uses fakes so no DB or real models are needed. Importing the | ||
| handler pulls DRF, which reads settings at import — configure a minimal | ||
| settings object when the suite hasn't already, so this runs standalone too. | ||
| """ | ||
|
|
||
| import django | ||
| from django.conf import settings | ||
|
|
||
| if not settings.configured: | ||
| settings.configure(INSTALLED_APPS=[], DATABASES={}) | ||
| django.setup() | ||
|
|
||
| from middleware.exception import _enrich_not_found_detail # noqa: E402 | ||
|
|
||
|
|
||
| class _Meta: | ||
| verbose_name = "lookup definition" | ||
|
|
||
|
|
||
| class _Model: | ||
| _meta = _Meta | ||
|
|
||
|
|
||
| class _QuerySet: | ||
| model = _Model | ||
|
|
||
|
|
||
| class _View: | ||
| queryset = _QuerySet | ||
|
|
||
|
|
||
| class _Resp: | ||
| def __init__(self, status_code, data): | ||
| self.status_code = status_code | ||
| self.data = data | ||
|
|
||
|
|
||
| def test_404_with_model_uses_verbose_name(): | ||
| resp = _Resp(404, {"errors": [{"code": "not_found", "detail": "Not found."}]}) | ||
| _enrich_not_found_detail(resp, {"view": _View()}) | ||
| assert resp.data["errors"][0]["detail"] == "Lookup definition not found." | ||
|
|
||
|
|
||
| def test_404_without_queryset_keeps_generic(): | ||
| resp = _Resp(404, {"errors": [{"code": "not_found", "detail": "Not found."}]}) | ||
| _enrich_not_found_detail(resp, {"view": object()}) | ||
| assert resp.data["errors"][0]["detail"] == "Not found." | ||
|
|
||
|
|
||
| def test_non_404_untouched(): | ||
| resp = _Resp(400, {"errors": [{"code": "not_found", "detail": "Not found."}]}) | ||
| _enrich_not_found_detail(resp, {"view": _View()}) | ||
| assert resp.data["errors"][0]["detail"] == "Not found." | ||
|
|
||
|
|
||
| def test_none_response_is_safe(): | ||
| _enrich_not_found_detail(None, {}) | ||
|
|
||
|
|
||
| def test_non_dict_error_item_does_not_raise(): | ||
| resp = _Resp( | ||
| 404, {"errors": ["unexpected", {"code": "not_found", "detail": "Not found."}]} | ||
| ) | ||
| _enrich_not_found_detail(resp, {"view": _View()}) | ||
| assert resp.data["errors"][1]["detail"] == "Lookup definition not found." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.