diff --git a/rest_framework/authtoken/views.py b/rest_framework/authtoken/views.py index 4f90d9528..7c03cb766 100644 --- a/rest_framework/authtoken/views.py +++ b/rest_framework/authtoken/views.py @@ -2,7 +2,6 @@ from rest_framework.views import APIView from rest_framework import status from rest_framework import parsers from rest_framework import renderers -from rest_framework import exceptions from rest_framework.response import Response from rest_framework.authtoken.models import Token from rest_framework.authtoken.serializers import AuthTokenSerializer @@ -21,7 +20,7 @@ class ObtainAuthToken(APIView): if serializer.is_valid(): token, created = Token.objects.get_or_create(user=serializer.object['user']) return Response({'token': token.key}) - raise exceptions.TokenAuthenticationError(serializer.errors) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) obtain_auth_token = ObtainAuthToken.as_view() diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 50e2959e0..dc5f686e9 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -27,20 +27,6 @@ class ParseError(APIException): default_detail = 'Malformed request.' -class DeserializeError(APIException): - status_code = status.HTTP_400_BAD_REQUEST - - def __init__(self, errors): - self.data = dict(errors) - - -class TokenAuthenticationError(DeserializeError): - """Raised when incorrect data is posted during Token Authentication.""" - # TODO: Change status code to HTTP_401 - # TODO: Make data look like {'detail': 'Reason of failure'} - pass - - class AuthenticationFailed(APIException): status_code = status.HTTP_401_UNAUTHORIZED default_detail = 'Incorrect authentication credentials.' diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index b190e5728..f11def6d4 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -7,7 +7,7 @@ which allows mixin classes to be composed in interesting ways. from __future__ import unicode_literals from django.http import Http404 -from rest_framework import status, exceptions +from rest_framework import status from rest_framework.response import Response from rest_framework.request import clone_request import warnings @@ -55,7 +55,7 @@ class CreateModelMixin(object): return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) - raise exceptions.DeserializeError(serializer.errors) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def get_success_headers(self, data): try: @@ -132,7 +132,7 @@ class UpdateModelMixin(object): self.post_save(self.object, created=created) return Response(serializer.data, status=success_status_code) - raise exceptions.DeserializeError(serializer.errors) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def partial_update(self, request, *args, **kwargs): kwargs['partial'] = True diff --git a/rest_framework/tests/test_views.py b/rest_framework/tests/test_views.py index 095788b42..aa1bef7a9 100644 --- a/rest_framework/tests/test_views.py +++ b/rest_framework/tests/test_views.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals import copy -import warnings from django.test import TestCase from django.test.client import RequestFactory @@ -94,10 +93,7 @@ class ClassBasedViewIntegrationTests(TestCase): def test_return_400_error(self): request = factory.post('/', '{"return_400_error": true}', content_type='application/json') - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') - response = self.view(request) - self.assertEqual(len(w), 1) + response = self.view(request) expected = { 'detail': 'Bad request' } @@ -146,10 +142,7 @@ class FunctionBasedViewIntegrationTests(TestCase): def test_return_400_error(self): request = factory.post('/', '{"return_400_error": true}', content_type='application/json') - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') - response = self.view(request) - self.assertEqual(len(w), 1) + response = self.view(request) expected = { 'detail': 'Bad request' } diff --git a/rest_framework/views.py b/rest_framework/views.py index 1304cebe7..4cb917b72 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -3,8 +3,6 @@ Provides an APIView class that is the base of all views in REST framework. """ from __future__ import unicode_literals -import warnings - from django.http import HttpResponse from django.utils.datastructures import SortedDict from django.views.decorators.csrf import csrf_exempt @@ -291,11 +289,6 @@ class APIView(View): response = handler(request, *args, **kwargs) - if response.status_code >= 400: - warnings.warn('Status code >= 400 returned. You should raise' - ' `rest_framework.exceptions.APIException`' - ' instead.', stacklevel=2) - except Exception as exc: response = self.handle_exception(exc)