Files
edx-platform/lms/djangoapps/discussion/django_comment_client/middleware.py
2019-06-19 08:35:43 +03:00

31 lines
1.0 KiB
Python

# pylint: disable=missing-docstring
from __future__ import absolute_import
import json
import logging
from six import text_type
from lms.djangoapps.discussion.django_comment_client.utils import JsonError
from openedx.core.djangoapps.django_comment_common.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(text_type(exception)), exception.status_code)
except ValueError:
return JsonError(text_type(exception), exception.status_code)
return None