From f6e4578656632c3a88b113c7349911a89a41bfea Mon Sep 17 00:00:00 2001 From: Andrew Straw Date: Wed, 29 Jun 2011 23:02:00 +0200 Subject: [PATCH] return 400 BAD REQUEST rather than Django traceback/500 error on failed multipart parse --- djangorestframework/parsers.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py index 3346a26e2..37882984c 100644 --- a/djangorestframework/parsers.py +++ b/djangorestframework/parsers.py @@ -13,6 +13,7 @@ We need a method to be able to: 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.response import ErrorResponse @@ -135,6 +136,10 @@ class MultiPartParser(BaseParser): `files` will be a :class:`QueryDict` containing all the form files. """ upload_handlers = self.view.request._get_upload_handlers() - django_parser = DjangoMultiPartParser(self.view.request.META, stream, upload_handlers) + try: + django_parser = DjangoMultiPartParser(self.view.request.META, stream, upload_handlers) + except MultiPartParserError, exc: + raise ErrorResponse(status.HTTP_400_BAD_REQUEST, + {'detail': 'multipart parse error - %s' % unicode(exc)}) return django_parser.parse()