CommentClientError now has sane subclasses that are meaningfully distinct, and each subclass is handled appropriately. Errors raised by the requests library are no longer handled by turning them into CommentClientErrors, since there is no meaningful handling we can do, and this way we will get more visibility into why errors are occurring. Also, HTTP status codes from the comments service indicating client error are correctly passed through to the client.
25 lines
893 B
Python
25 lines
893 B
Python
from comment_client import CommentClientRequestError
|
|
from django_comment_client.utils import JsonError
|
|
import json
|
|
import logging
|
|
|
|
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
|