Commit 52184387 by Calen Pennington

Make the django_comment_client return errors that can't be parsed as JSON just…

Make the django_comment_client return errors that can't be parsed as JSON just as simple strings when in an ajax context
parent f70511eb
from comment_client import CommentClientError from comment_client import CommentClientError
from django_comment_client.utils import JsonError from django_comment_client.utils import JsonError
import json import json
import logging
log = logging.getLogger(__name__)
class AjaxExceptionMiddleware(object): class AjaxExceptionMiddleware(object):
"""
Middleware that captures CommentClientErrors during ajax requests
and tranforms them into json responses
"""
def process_exception(self, request, exception): def process_exception(self, request, exception):
"""
Processes CommentClientErrors in ajax requests. If the request is an ajax request,
returns a http response that encodes the error as json
"""
if isinstance(exception, CommentClientError) and request.is_ajax(): if isinstance(exception, CommentClientError) and request.is_ajax():
return JsonError(json.loads(exception.message)) try:
return JsonError(json.loads(exception.message))
except ValueError:
return JsonError(exception.message)
return None return None
import string
import random
import collections
from django.test import TestCase from django.test import TestCase
import comment_client import comment_client
...@@ -13,17 +9,19 @@ class AjaxExceptionTestCase(TestCase): ...@@ -13,17 +9,19 @@ class AjaxExceptionTestCase(TestCase):
# TODO: check whether the correct error message is produced. # TODO: check whether the correct error message is produced.
# The error message should be the same as the argument to CommentClientError # The error message should be the same as the argument to CommentClientError
def setUp(self): def setUp(self):
self.a = middleware.AjaxExceptionMiddleware() self.a = middleware.AjaxExceptionMiddleware()
self.request1 = django.http.HttpRequest() self.request1 = django.http.HttpRequest()
self.request0 = django.http.HttpRequest() self.request0 = django.http.HttpRequest()
self.exception1 = comment_client.CommentClientError('{}') self.exception1 = comment_client.CommentClientError('{}')
self.exception0 = ValueError() self.exception2 = comment_client.CommentClientError('Foo!')
self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest" self.exception0 = ValueError()
self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX" self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX"
def test_process_exception(self): def test_process_exception(self):
self.assertIsInstance(self.a.process_exception(self.request1, self.exception1), middleware.JsonError) self.assertIsInstance(self.a.process_exception(self.request1, self.exception1), middleware.JsonError)
self.assertIsNone(self.a.process_exception(self.request1, self.exception0)) self.assertIsInstance(self.a.process_exception(self.request1, self.exception2), middleware.JsonError)
self.assertIsNone(self.a.process_exception(self.request0, self.exception1)) self.assertIsNone(self.a.process_exception(self.request1, self.exception0))
self.assertIsNone(self.a.process_exception(self.request0, self.exception0)) self.assertIsNone(self.a.process_exception(self.request0, self.exception1))
self.assertIsNone(self.a.process_exception(self.request0, self.exception0))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment