Skip to content

Fix exception causes in relations.py #7378

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ def get_object(self, view_name, view_args, view_kwargs):

try:
return queryset.get(**lookup_kwargs)
except ValueError:
except ValueError as e:
exc = ObjectValueError(str(sys.exc_info()[1]))
raise exc.with_traceback(sys.exc_info()[2])
except TypeError:
raise exc.with_traceback(sys.exc_info()[2]) from e
except TypeError as e:
exc = ObjectTypeError(str(sys.exc_info()[1]))
raise exc.with_traceback(sys.exc_info()[2])
raise exc.with_traceback(sys.exc_info()[2]) from e

def get_url(self, obj, view_name, request, format):
"""
Expand Down Expand Up @@ -391,7 +391,7 @@ def to_representation(self, value):
# Return the hyperlink, or error if incorrectly configured.
try:
url = self.get_url(value, self.view_name, request, format)
except NoReverseMatch:
except NoReverseMatch as e:
msg = (
'Could not resolve URL for hyperlinked relationship using '
'view name "%s". You may have failed to include the related '
Expand All @@ -405,7 +405,7 @@ def to_representation(self, value):
"was %s, which may be why it didn't match any "
"entries in your URL conf." % value_string
)
raise ImproperlyConfigured(msg % self.view_name)
raise ImproperlyConfigured(msg % self.view_name) from e

if url is None:
return None
Expand Down