Files
edx-platform/lms/djangoapps/django_comment_client/middleware.py
2017-06-11 21:48:06 -04:00

26 lines
902 B
Python

import json
import logging
from django_comment_client.utils import JsonError
from lms.lib.comment_client import CommentClientRequestError
log = logging.getLogger(__name__)
class AjaxExceptionMiddleware(object):
"""
Middleware that captures CommentClientRequestErrors during ajax requests
and tranforms them into json responses
"""
def process_exception(self, request, exception):
"""
Processes CommentClientRequestErrors in ajax requests. If the request is an ajax request,
returns a http response that encodes the error as json
"""
if isinstance(exception, CommentClientRequestError) and request.is_ajax():
try:
return JsonError(json.loads(exception.message), exception.status_code)
except ValueError:
return JsonError(exception.message, exception.status_code)
return None