mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Add ParseError (Removing ImmediateResponse)
This commit is contained in:
parent
aed26b218e
commit
26831df88e
3
djangorestframework/exceptions.py
Normal file
3
djangorestframework/exceptions.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
class ParseError(Exception):
|
||||
def __init__(self, detail):
|
||||
self.detail = detail
|
|
@ -15,9 +15,8 @@ from django.http import QueryDict
|
|||
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
||||
from django.http.multipartparser import MultiPartParserError
|
||||
from django.utils import simplejson as json
|
||||
from djangorestframework import status
|
||||
from djangorestframework.compat import yaml
|
||||
from djangorestframework.response import ImmediateResponse
|
||||
from djangorestframework.exceptions import ParseError
|
||||
from djangorestframework.utils.mediatypes import media_type_matches
|
||||
from xml.etree import ElementTree as ET
|
||||
from djangorestframework.compat import ETParseError
|
||||
|
@ -83,9 +82,7 @@ class JSONParser(BaseParser):
|
|||
try:
|
||||
return (json.load(stream), None)
|
||||
except ValueError, exc:
|
||||
raise ImmediateResponse(
|
||||
{'detail': 'JSON parse error - %s' % unicode(exc)},
|
||||
status=status.HTTP_400_BAD_REQUEST)
|
||||
raise ParseError('JSON parse error - %s' % unicode(exc))
|
||||
|
||||
|
||||
class YAMLParser(BaseParser):
|
||||
|
@ -105,9 +102,7 @@ class YAMLParser(BaseParser):
|
|||
try:
|
||||
return (yaml.safe_load(stream), None)
|
||||
except (ValueError, yaml.parser.ParserError), exc:
|
||||
raise ImmediateResponse(
|
||||
{'detail': 'YAML parse error - %s' % unicode(exc)},
|
||||
status=status.HTTP_400_BAD_REQUEST)
|
||||
raise ParseError('YAML parse error - %s' % unicode(exc))
|
||||
|
||||
|
||||
class PlainTextParser(BaseParser):
|
||||
|
@ -163,9 +158,7 @@ class MultiPartParser(BaseParser):
|
|||
parser = DjangoMultiPartParser(meta, stream, upload_handlers)
|
||||
return parser.parse()
|
||||
except MultiPartParserError, exc:
|
||||
raise ImmediateResponse(
|
||||
{'detail': 'multipart parse error - %s' % unicode(exc)},
|
||||
status=status.HTTP_400_BAD_REQUEST)
|
||||
raise ParseError('Multipart form parse error - %s' % unicode(exc))
|
||||
|
||||
|
||||
class XMLParser(BaseParser):
|
||||
|
@ -185,8 +178,7 @@ class XMLParser(BaseParser):
|
|||
try:
|
||||
tree = ET.parse(stream)
|
||||
except (ExpatError, ETParseError, ValueError), exc:
|
||||
content = {'detail': 'XML parse error - %s' % unicode(exc)}
|
||||
raise ImmediateResponse(content, status=status.HTTP_400_BAD_REQUEST)
|
||||
raise ParseError('XML parse error - %s' % unicode(exc))
|
||||
data = self._xml_convert(tree.getroot())
|
||||
|
||||
return (data, None)
|
||||
|
|
|
@ -13,7 +13,7 @@ from django.views.decorators.csrf import csrf_exempt
|
|||
from djangorestframework.compat import View as DjangoView, apply_markdown
|
||||
from djangorestframework.response import Response, ImmediateResponse
|
||||
from djangorestframework.request import Request
|
||||
from djangorestframework import renderers, parsers, authentication, permissions, status
|
||||
from djangorestframework import renderers, parsers, authentication, permissions, status, exceptions
|
||||
|
||||
|
||||
__all__ = (
|
||||
|
@ -249,6 +249,8 @@ class View(DjangoView):
|
|||
|
||||
except ImmediateResponse, exc:
|
||||
response = exc.response
|
||||
except exceptions.ParseError as exc:
|
||||
response = Response({'detail': exc.detail}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
self.response = self.final(request, response, *args, **kwargs)
|
||||
return self.response
|
||||
|
|
Loading…
Reference in New Issue
Block a user