Skip to content

Instantly share code, notes, and snippets.

@satran
Created July 16, 2012 07:22
Show Gist options
  • Save satran/3121331 to your computer and use it in GitHub Desktop.
Save satran/3121331 to your computer and use it in GitHub Desktop.
JSON exception Middleware
from django.http import HttpResponseServerError
import json
import traceback
import sys
class JSONException(object):
'''
Return all exceptions as a JSON object.
'''
def process_exception(self, request, exception):
exc_traceback = sys.exc_info()[-1]
response = {"error": str(exception)}
response['details'] = traceback.extract_tb(exc_traceback)
return HttpResponseServerError(json.dumps(response), content_type='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment