Files
edx-platform/lms/djangoapps/discussion/django_comment_client/middleware.py
edX requirements bot f33f12bbea BOM-2358 : Pyupgrade in dashboard, debug, discussion apps (#26529)
* Python code cleanup by the cleanup-python-code Jenkins job.

This pull request was generated by the cleanup-python-code Jenkins job, which ran
```
cd lms/djangoapps/dashboard; find . -type f -name '*.py' | while read fname; do sed -i 's/  # lint-amnesty, pylint: disable=super-with-arguments//; s/  # lint-amnesty, pylint: disable=import-error, wrong-import-order//; s/  # lint-amnesty, pylint: disable=wrong-import-order//' "$fname"; done; find . -type f -name '*.py' | while read fname; do pyupgrade --exit-zero-even-if-changed --py3-plus --py36-plus --py38-plus "$fname"; done; isort --recursive .
```

The following packages were installed:
`pyupgrade,isort`

* feedback done

Co-authored-by: Zulqarnain <muhammad.zulqarnain@arbisoft.com>
2021-02-22 15:42:21 +05:00

29 lines
1.1 KiB
Python

# lint-amnesty, pylint: disable=missing-module-docstring
import json
import logging
from django.utils.deprecation import MiddlewareMixin
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(MiddlewareMixin):
"""
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(str(exception)), exception.status_code)
except ValueError:
return JsonError(str(exception), exception.status_code)
return None