diff --git a/jsonit/decorators.py b/jsonit/decorators.py index ebcc5df..42f7dd0 100644 --- a/jsonit/decorators.py +++ b/jsonit/decorators.py @@ -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 diff --git a/jsonit/http.py b/jsonit/http.py index 0dc31d3..315334a 100644 --- a/jsonit/http.py +++ b/jsonit/http.py @@ -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