mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Add error handling for a raised AttributeError inside of a property call for "request.user".
This commit is contained in:
parent
36fe656b51
commit
75d755a8fc
|
@ -223,7 +223,10 @@ class Request(object):
|
|||
by the authentication classes provided to the request.
|
||||
"""
|
||||
if not hasattr(self, '_user'):
|
||||
self._authenticate()
|
||||
try:
|
||||
self._authenticate()
|
||||
except AttributeError as error:
|
||||
raise RuntimeError("AttributeError: " + error.message)
|
||||
return self._user
|
||||
|
||||
@user.setter
|
||||
|
|
|
@ -333,24 +333,27 @@ class TestUserSetter(TestCase):
|
|||
login(self.request, self.user)
|
||||
self.assertEqual(self.request.user, self.user)
|
||||
|
||||
def test_calling_user_fails_when_exception_is_raised(self):
|
||||
class AuthRaisesError(object):
|
||||
def test_calling_user_fails_when_attribute_error_is_raised(self):
|
||||
"""
|
||||
This proves that when an AttributeError is raised inside of the request.user
|
||||
property, that we can handle this and report the true, underlying error.
|
||||
"""
|
||||
class AuthRaisesAttributeError(object):
|
||||
def authenticate(self, request):
|
||||
raise AttributeError('We should see this error!')
|
||||
# import rest_framework
|
||||
# rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST
|
||||
import rest_framework
|
||||
rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST
|
||||
|
||||
self.request = Request(factory.get('/'), authenticators=(AuthRaisesError(),))
|
||||
self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),))
|
||||
SessionMiddleware().process_request(self.request)
|
||||
|
||||
login(self.request, self.user)
|
||||
error_seen = None
|
||||
try:
|
||||
self.request.user
|
||||
except AttributeError as error:
|
||||
except RuntimeError as error:
|
||||
error_seen = error
|
||||
|
||||
self.assertEqual('We should see this error!', error_seen.message)
|
||||
self.assertEqual("AttributeError: 'module' object has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'", error_seen.message)
|
||||
|
||||
def test_user_can_logout(self):
|
||||
self.request.user = self.user
|
||||
|
|
Loading…
Reference in New Issue
Block a user