Created
November 27, 2012 18:43
-
-
Save minism/4156153 to your computer and use it in GitHub Desktop.
Tastypie serializer to return 400 on bad JSON request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tastypie.serializers import Serializer | |
from tastypie.exceptions import BadRequest | |
class JSONClientErrorSerializer(Serializer): | |
""" | |
Small override to tastypie's internal serializer to provide a client error | |
on a malformed JSON request body | |
""" | |
def from_json(self, *args, **kwargs): | |
try: | |
return super(JSONClientErrorSerializer, self).from_json(*args, **kwargs) | |
except ValueError: | |
raise BadRequest("malformed JSON request") | |
# usage | |
from tastypie.resources import ModelResource | |
class MyResource(ModelResource): | |
class Meta: | |
serializer = JSONClientErrorSerializer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment