<commit_before>import logging
import textwrap

logger = logging.getLogger(__name__)


class GlobusError(Exception):
    """
    Root of the Globus Exception hierarchy.
    Stub class.
    """


class GlobusAPIError(GlobusError):
    """
    Wraps errors returned by a REST API.

    :ivar http_status: HTTP status code (int)
    :ivar code: Error code from the API (str),
                or "Error" for unclassified errors
    :ivar message: Error message from the API. In general, this will be more
                   useful to developers, but there may be cases where it's
                   suitable for display to end users.
    """
    def __init__(self, r, *args, **kw):
        self._underlying_response = r
        self.http_status = r.status_code
        if "Content-Type" in r.headers and (
                "application/json" in r.headers["Content-Type"]):
            logger.debug(('Content-Type on error is application/json. '
                          'Doing error load from JSON'))
            try:
                self._load_from_json(r.json())
            except KeyError:
                logger.error(('Error body could not be JSON decoded! '
                              'This means the Content-Type is wrong, or the '
                              'body is malformed!'))
                self._load_from_text(r.text)
        else:
            logger.debug(('Content-Type on error is unknown. '
                          'Failing over to error load as text (default)'))
            # fallback to using the entire body as the message for all
            # other types
            self._load_from_text(r.text)
        args = self._get_args()
        GlobusError.__init__(self, *args)

    def _get_args(self):
        """
        Get arguments to pass to the Exception base class. These args are
        displayed in stack traces.
        """
        return (self.http_status, self.code, self.message)
<commit_msg>Add raw_{json,text} properties to GlobusAPIError<commit_after>import logging
import textwrap

logger = logging.getLogger(__name__)


class GlobusError(Exception):
    """
    Root of the Globus Exception hierarchy.
    Stub class.
    """


class GlobusAPIError(GlobusError):
    """
    Wraps errors returned by a REST API.

    :ivar http_status: HTTP status code (int)
    :ivar code: Error code from the API (str),
                or "Error" for unclassified errors
    :ivar message: Error message from the API. In general, this will be more
                   useful to developers, but there may be cases where it's
                   suitable for display to end users.
    """
    def __init__(self, r, *args, **kw):
        self._underlying_response = r
        self.http_status = r.status_code
        if "Content-Type" in r.headers and (
                "application/json" in r.headers["Content-Type"]):
            logger.debug(('Content-Type on error is application/json. '
                          'Doing error load from JSON'))
            try:
                self._load_from_json(r.json())
            except KeyError:
                logger.error(('Error body could not be JSON decoded! '
                              'This means the Content-Type is wrong, or the '
                              'body is malformed!'))
                self._load_from_text(r.text)
        else:
            logger.debug(('Content-Type on error is unknown. '
                          'Failing over to error load as text (default)'))
            # fallback to using the entire body as the message for all
            # other types
            self._load_from_text(r.text)
        args = self._get_args()
        GlobusError.__init__(self, *args)

    @property
    def raw_json(self):
        """
        Get the verbatim error message received from a Globus API, interpreted
        as a JSON string and evaluated as a *dict*

        If the body cannot be loaded as JSON, this is None
        """
        r = self._underlying_response
        if "Content-Type" in r.headers and (
                "application/json" in r.headers["Content-Type"]):
            return r.json()
        else:
            return None

    @property
    def raw_text(self):
        """
        Get the verbatim error message receved from a Globus API as a *string*
        """
        return self._underlying_response.text

    def _get_args(self):
        """
        Get arguments to pass to the Exception base class. These args are
        displayed in stack traces.
        """
        return (self.http_status, self.code, self.message)

{'fraction_non_alphanumeric': 0.08283893649747308, 'fraction_numerical': 0.0, 'mean_word_length': 1.816831683168317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4846872', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1195, 'n_words': 426}