From 900013304312def597ccc89f795955fb0cb48c12 Mon Sep 17 00:00:00 2001 From: Travis Jensen Date: Sun, 25 Nov 2012 14:52:12 -0700 Subject: [PATCH] Allow HttpResponse objects to be returned from ajax functions Allowing HttpResponse objects allows ajax functions (or decorators) to implement better error handling (such as a 401 for Unauthorized), which follows the HTTP specification more closely. Make sure that the proper response codes are being returned for error conditions. --- dajaxice/tests/__init__.py | 2 +- dajaxice/views.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dajaxice/tests/__init__.py b/dajaxice/tests/__init__.py index fb918e5..f4bd058 100644 --- a/dajaxice/tests/__init__.py +++ b/dajaxice/tests/__init__.py @@ -137,7 +137,7 @@ def test_calling_registered_function_with_params(self): def test_bad_function(self): response = self.client.post('/dajaxice/dajaxice.tests.test_ajax_exception/') - self.failUnlessEqual(response.status_code, 200) + self.failUnlessEqual(response.status_code, 500) self.failUnlessEqual(response.content, "DAJAXICE_EXCEPTION") def test_get_register(self): diff --git a/dajaxice/views.py b/dajaxice/views.py index 63911ec..d9afb55 100644 --- a/dajaxice/views.py +++ b/dajaxice/views.py @@ -48,13 +48,18 @@ def dispatch(self, request, name=None): data = {} # Call the function. If something goes wrong, handle the Exception + status = 200 try: response = function.call(request, **data) except Exception: if settings.DEBUG: raise response = dajaxice_config.DAJAXICE_EXCEPTION + status = 500 - return HttpResponse(response, mimetype="application/x-json") + if isinstance(response, basestring): + return HttpResponse(response, status=status, mimetype="application/json") + else: + return response else: raise FunctionNotCallableError(name)