diff --git a/rest_framework/request.py b/rest_framework/request.py index 275326614..84ac9183c 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -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 diff --git a/tests/test_request.py b/tests/test_request.py index 3b72bd22d..75fc120db 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -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