Skip to content

Avoided shadowing of exception when rendering errors #1220

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 1 commit into from
Apr 18, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ any parts of the framework not mentioned in the documentation should generally b
* `ModelSerializer` fields are now returned in the same order than DRF
* Avoided that an empty attributes dict is rendered in case serializer does not
provide any attribute fields.
* Avoided shadowing of exception when rendering errors (regression since 4.3.0).

### Removed

Expand Down
13 changes: 7 additions & 6 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,19 @@ def format_drf_errors(response, context, exc):
errors.extend(format_error_object(message, "/data", response))
# handle all errors thrown from serializers
else:
# Avoid circular deps
from rest_framework import generics

has_serializer = isinstance(context["view"], generics.GenericAPIView)
if has_serializer:
try:
serializer = context["view"].get_serializer()
fields = get_serializer_fields(serializer) or dict()
relationship_fields = [
format_field_name(name)
for name, field in fields.items()
if is_relationship_field(field)
]
except Exception:
# ignore potential errors when retrieving serializer
# as it might shadow error which is currently being
# formatted
serializer = None

for field, error in response.data.items():
non_field_error = field == api_settings.NON_FIELD_ERRORS_KEY
Expand All @@ -401,7 +402,7 @@ def format_drf_errors(response, context, exc):
if non_field_error:
# Serializer error does not refer to a specific field.
pointer = "/data"
elif has_serializer:
elif serializer:
# pointer can be determined only if there's a serializer.
rel = "relationships" if field in relationship_fields else "attributes"
pointer = f"/data/{rel}/{field}"
Expand Down
11 changes: 11 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ def test_list_with_include_nested_related_field(
"included"
]

@pytest.mark.urls(__name__)
def test_list_with_invalid_include(self, client, foreign_key_source):
url = reverse("foreign-key-source-list")
response = client.get(url, data={"include": "invalid"})
assert response.status_code == status.HTTP_400_BAD_REQUEST
result = response.json()
assert (
result["errors"][0]["detail"]
== "This endpoint does not support the include parameter for path invalid"
)

@pytest.mark.urls(__name__)
def test_list_with_default_included_resources(self, client, foreign_key_source):
url = reverse("default-included-resources-list")
Expand Down
Loading