From 442e75a114e6477d1fc08fb3d84746d5a0694f14 Mon Sep 17 00:00:00 2001 From: "Paulo R. Macedo Chaves" Date: Fri, 21 Sep 2012 02:11:35 -0300 Subject: [PATCH 1/2] Add support to return traceback. There is a settings values JSONIT_TRACEBACK that holds the option to return the traceback (more information) than the exception (no useful information) on decorator catch_ajax_exceptions --- jsonit/decorators.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/jsonit/decorators.py b/jsonit/decorators.py index ebcc5df..cf75b54 100644 --- a/jsonit/decorators.py +++ b/jsonit/decorators.py @@ -1,23 +1,34 @@ 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 + From c810273a7154e34adccd3bb31d9674dcc12ed9a9 Mon Sep 17 00:00:00 2001 From: "Paulo R. Macedo Chaves" Date: Fri, 25 Jan 2013 14:35:30 -0200 Subject: [PATCH 2/2] Add support to jsonp/callback --- jsonit/decorators.py | 1 - jsonit/http.py | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/jsonit/decorators.py b/jsonit/decorators.py index cf75b54..42f7dd0 100644 --- a/jsonit/decorators.py +++ b/jsonit/decorators.py @@ -31,4 +31,3 @@ def dec(request, *args, **kwargs): 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