Skip to content
This repository was archived by the owner on Apr 6, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions jsonit/decorators.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
from functools import wraps
import traceback

from django.conf import settings

from jsonit.http import JSONResponse


JSONIT_TRACEBACK = getattr(settings, "JSONIT_TRACEBACK", False)


def catch_ajax_exceptions(func):
"""
Catches exceptions which occur when using an AJAX request.
Catches exceptions which occur when using an AJAX request.

These exceptions will be returned using a :class:`JSONResponse` rather than
letting the exception propogate.
"""

@wraps(func)
def dec(request, *args, **kwargs):
try:
return func(request, *args, **kwargs)
except Exception, e:
if request.is_ajax():
return JSONResponse(request, exception=e)
if JSONIT_TRACEBACK:
return JSONResponse(
request, exception=traceback.format_exc())
else:
return JSONResponse(request, exception=e)
raise

return dec
6 changes: 5 additions & 1 deletion jsonit/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def build_json(self, exception=None):
if self.extra_context:
content['extra_context'] = self.extra_context
try:
return encode(content)
encoded = encode(content)
if self.request.GET.get("format") == 'jsonp':
callback = self.request.GET.get("callback", "alert")
return u"%s(%s)" % (callback, encoded)
return encoded
except Exception, e:
if exception is not None:
raise
Expand Down