Previously, AJAX calls would return 400, and page views and attempts to load inline discussions would return 404 if communication with the comments service failed. Now such problems cause a 500 status code.
25 lines
829 B
Python
25 lines
829 B
Python
from comment_client import CommentClientError
|
|
from django_comment_client.utils import JsonError
|
|
import json
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class AjaxExceptionMiddleware(object):
|
|
"""
|
|
Middleware that captures CommentClientErrors during ajax requests
|
|
and tranforms them into json responses
|
|
"""
|
|
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():
|
|
try:
|
|
return JsonError(json.loads(exception.message), 500)
|
|
except ValueError:
|
|
return JsonError(exception.message, 500)
|
|
return None
|