Skip to content

Commit b8a200e

Browse files
author
Ryan P Kilby
committed
Only catch TypeError/ValueError for object lookups
1 parent fe54575 commit b8a200e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

rest_framework/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def __hash__(self):
9595
return hash(str(self))
9696

9797

98+
class ObjectValueError(Exception):
99+
"""
100+
A value was not suitable for requesting an object.
101+
"""
102+
103+
98104
class APIException(Exception):
99105
"""
100106
Base class for REST framework exceptions.

rest_framework/relations.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.utils.six.moves.urllib import parse as urlparse
1515
from django.utils.translation import ugettext_lazy as _
1616

17+
from rest_framework.exceptions import ObjectValueError
1718
from rest_framework.fields import (
1819
Field, empty, get_attribute, is_simple_callable, iter_options
1920
)
@@ -296,7 +297,12 @@ def get_object(self, view_name, view_args, view_kwargs):
296297
"""
297298
lookup_value = view_kwargs[self.lookup_url_kwarg]
298299
lookup_kwargs = {self.lookup_field: lookup_value}
299-
return self.get_queryset().get(**lookup_kwargs)
300+
queryset = self.get_queryset()
301+
302+
try:
303+
return queryset.get(**lookup_kwargs)
304+
except (TypeError, ValueError) as exc:
305+
six.raise_from(ObjectValueError(), exc)
300306

301307
def get_url(self, obj, view_name, request, format):
302308
"""
@@ -346,7 +352,7 @@ def to_internal_value(self, data):
346352

347353
try:
348354
return self.get_object(match.view_name, match.args, match.kwargs)
349-
except (ObjectDoesNotExist, TypeError, ValueError):
355+
except (ObjectDoesNotExist, ObjectValueError):
350356
self.fail('does_not_exist')
351357

352358
def to_representation(self, value):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy