<commit_before>from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponseBadRequest

from temba_client.base import TembaAPIError, TembaConnectionError


class HandleTembaAPIError(object):
    """ Catch all Temba exception errors """

    def process_exception(self, request, exception):

        rapidProConnectionErrorString = _(
            "RapidPro appears to be down right now. "
            "Please try again later.")

        if isinstance(exception, TembaAPIError):
            rapidpro_connection_error_codes = ["502", "503", "504"]

            if any(code in exception.caused_by.message for code in rapidpro_connection_error_codes):
                return HttpResponseBadRequest(
                    rapidProConnectionErrorString)

            else:
                return HttpResponseBadRequest(
                    _("Org does not have a valid API Key. "
                      "Please edit the org through Site Manage or contact your administrator."))

        elif isinstance(exception, TembaConnectionError):
            return HttpResponseBadRequest(
                rapidProConnectionErrorString)

        pass<commit_msg>Minor edits to exception handling<commit_after>from django.utils.translation import ugettext_lazy as _
from django.http import HttpResponseBadRequest

from requests import HTTPError

from temba_client.base import TembaAPIError, TembaConnectionError


class HandleTembaAPIError(object):
    """ Catch all Temba exception errors """

    def process_exception(self, request, exception):

        rapidProConnectionErrorString = _(
            "RapidPro appears to be down right now. "
            "Please try again later.")

        if isinstance(exception, TembaAPIError):
            if isinstance(exception.caused_by, HTTPError):
                response = exception.caused_by.response
                if response is not None:
                    if response.status_code == 403:
                        return HttpResponseBadRequest(
                            _("Org does not have a valid API Key. "
                              "Please edit the org through Site Manage or contact your administrator."))

                    elif response.status_code >= 500:
                        return HttpResponseBadRequest(rapidProConnectionErrorString)

        elif isinstance(exception, TembaConnectionError):
            return HttpResponseBadRequest(
                rapidProConnectionErrorString)

        return None
{'fraction_non_alphanumeric': 0.05622489959839357, 'fraction_numerical': 0.006024096385542169, 'mean_word_length': 2.3526244952893673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5754357', 'n_tokens_mistral': 607, 'n_tokens_neox': 565, 'n_words': 178}