Skip to content

Instantly share code, notes, and snippets.

@jazztpt
Created June 7, 2011 23:38
Show Gist options
  • Save jazztpt/1013453 to your computer and use it in GitHub Desktop.
Save jazztpt/1013453 to your computer and use it in GitHub Desktop.
Valentunes wrap_view
def wrap_view(self, view):
@csrf_exempt
def wrapper(request, *args, **kwargs):
try:
...(bunch of standard stuff here)...
return response
except (BadRequest, ApiFieldError), e:
message = e.args[0]
return json_response({ 'code' : '14',
'message' : message })
except ValidationError, e:
message = ', '.join(e.messages)
return json_response({ 'code' : '12',
'message' : message })
except Exception, e:
if hasattr(e, 'response'):
# 401 is the HTTP status code for Unauthorized, so we explicitly inform the user of this error.
if e.response.status_code == 401:
return json_response({ 'code' : '3',
'message' : 'Bad username/password.'})
else:
message = ', '.join(e.messages)
return json_response({ 'code': '14',
'message' : message })
# A real, non-expected exception.
# Handle the case where the full traceback is more helpful
# than the serialized error.
if settings.DEBUG and getattr(settings, 'TASTYPIE_FULL_DEBUG', False):
raise
# Rather than re-raising, we're going to things similar to
# what Django does. The difference is returning a serialized
# error message.
return self._handle_500(request, e)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment