Remove requirement of raising exceptions when returning non-400 response

This commit is contained in:
David Avsajanishvili 2013-06-05 23:21:53 +04:00
parent 74643b08b6
commit f9dacd79b8
5 changed files with 6 additions and 35 deletions

View File

@ -2,7 +2,6 @@ from rest_framework.views import APIView
from rest_framework import status from rest_framework import status
from rest_framework import parsers from rest_framework import parsers
from rest_framework import renderers from rest_framework import renderers
from rest_framework import exceptions
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from rest_framework.authtoken.serializers import AuthTokenSerializer from rest_framework.authtoken.serializers import AuthTokenSerializer
@ -21,7 +20,7 @@ class ObtainAuthToken(APIView):
if serializer.is_valid(): if serializer.is_valid():
token, created = Token.objects.get_or_create(user=serializer.object['user']) token, created = Token.objects.get_or_create(user=serializer.object['user'])
return Response({'token': token.key}) 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() obtain_auth_token = ObtainAuthToken.as_view()

View File

@ -27,20 +27,6 @@ class ParseError(APIException):
default_detail = 'Malformed request.' 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): class AuthenticationFailed(APIException):
status_code = status.HTTP_401_UNAUTHORIZED status_code = status.HTTP_401_UNAUTHORIZED
default_detail = 'Incorrect authentication credentials.' default_detail = 'Incorrect authentication credentials.'

View File

@ -7,7 +7,7 @@ which allows mixin classes to be composed in interesting ways.
from __future__ import unicode_literals from __future__ import unicode_literals
from django.http import Http404 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.response import Response
from rest_framework.request import clone_request from rest_framework.request import clone_request
import warnings import warnings
@ -55,7 +55,7 @@ class CreateModelMixin(object):
return Response(serializer.data, status=status.HTTP_201_CREATED, return Response(serializer.data, status=status.HTTP_201_CREATED,
headers=headers) headers=headers)
raise exceptions.DeserializeError(serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_success_headers(self, data): def get_success_headers(self, data):
try: try:
@ -132,7 +132,7 @@ class UpdateModelMixin(object):
self.post_save(self.object, created=created) self.post_save(self.object, created=created)
return Response(serializer.data, status=success_status_code) 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): def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True kwargs['partial'] = True

View File

@ -1,7 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import copy import copy
import warnings
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
@ -94,10 +93,7 @@ class ClassBasedViewIntegrationTests(TestCase):
def test_return_400_error(self): def test_return_400_error(self):
request = factory.post('/', '{"return_400_error": true}', request = factory.post('/', '{"return_400_error": true}',
content_type='application/json') content_type='application/json')
with warnings.catch_warnings(record=True) as w: response = self.view(request)
warnings.simplefilter('always')
response = self.view(request)
self.assertEqual(len(w), 1)
expected = { expected = {
'detail': 'Bad request' 'detail': 'Bad request'
} }
@ -146,10 +142,7 @@ class FunctionBasedViewIntegrationTests(TestCase):
def test_return_400_error(self): def test_return_400_error(self):
request = factory.post('/', '{"return_400_error": true}', request = factory.post('/', '{"return_400_error": true}',
content_type='application/json') content_type='application/json')
with warnings.catch_warnings(record=True) as w: response = self.view(request)
warnings.simplefilter('always')
response = self.view(request)
self.assertEqual(len(w), 1)
expected = { expected = {
'detail': 'Bad request' 'detail': 'Bad request'
} }

View File

@ -3,8 +3,6 @@ Provides an APIView class that is the base of all views in REST framework.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
@ -291,11 +289,6 @@ class APIView(View):
response = handler(request, *args, **kwargs) 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: except Exception as exc:
response = self.handle_exception(exc) response = self.handle_exception(exc)