<commit_before># Copyright 2014, 2015 IBM Corp.
#
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Module-specific error/exception subclasses."""

import abc
from lxml import etree
import six

from pypowervm import const as c
from pypowervm import entities as ent
from pypowervm.i18n import _


class Error(Exception):
    """Error on PowerVM API Adapter method invocation."""

    def __init__(self, msg, response=None):
        """Initializes Error with optional `response` object."""
        self.response = response
        self.orig_response = None
        super(Error, self).__init__(msg)


class ConnectionError(Error):
    """Connection Error on PowerVM API Adapter method invocation."""


class SSLError(Error):
    """SSL Error on PowerVM API Adapter method invocation."""


class TimeoutError(Error):
    """Timeout Error on PowerVM API Adapter method invocation."""


class HttpError(Error):
    """HTTP Error on PowerVM API Adapter method invocation."""

    def __init__(self, resp):
        """Initializes HttpError with required `response` object.

        1) Constructs the exception message based on the contents of the
        response parameter.

        2) If possible, initializes a 'her_wrap' member which is a
        pypowervm.wrappers.http_error.HttpError - an EntryWrapper for the
        <HttpErrorResponse/> payload.  Consumers should check this member for
        None before using.

        :param resp: pypowervm.adapter.Response containing an
                     <HttpErrorResponse/> response from the REST server.
        """
        self.her_wrap = None
        reason = resp.reason
        # Attempt to extract PowerVM API's HttpErrorResponse object.
        # Since this is an exception path, we use best effort only - don't want
        # problems here to obscure the real exception.
        try:
            root = etree.fromstring(resp.body)
            if root is not None and root.tag == str(etree.QName(c.ATOM_NS,
                                                                'entry')):
                resp.entry = ent.Entry.unmarshal_atom_entry(root, resp)
                # Import inline to avoid circular dependencies
                import pypowervm.wrappers.http_error as he
                self.her_wrap = he.HttpError.wrap(resp)
                # For 500, the useful information is in the 'message'.
                if resp.status == c.HTTPStatus.INTERNAL_ERROR:
                    reason += ' -- ' + self.her_wrap.message
        except Exception:
            pass
        # Construct the exception message
        msg = ('HTTP error %(status)s for method %(method)s on path '
               '%(path)s: %(reason)s') % dict(status=resp.status,
                                              method=resp.reqmethod,
                                              path=resp.reqpath,
                                              reason=reason)
        # Initialize the exception
        super(HttpError, self).__init__(msg, response=resp)
<commit_msg>Add messages to 4XX exceptions<commit_after># Copyright 2014, 2015 IBM Corp.
#
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Module-specific error/exception subclasses."""

import abc
from lxml import etree
import six

from pypowervm import const as c
from pypowervm import entities as ent
from pypowervm.i18n import _


class Error(Exception):
    """Error on PowerVM API Adapter method invocation."""

    def __init__(self, msg, response=None):
        """Initializes Error with optional `response` object."""
        self.response = response
        self.orig_response = None
        super(Error, self).__init__(msg)


class ConnectionError(Error):
    """Connection Error on PowerVM API Adapter method invocation."""


class SSLError(Error):
    """SSL Error on PowerVM API Adapter method invocation."""


class TimeoutError(Error):
    """Timeout Error on PowerVM API Adapter method invocation."""


class HttpError(Error):
    """HTTP Error on PowerVM API Adapter method invocation."""

    def __init__(self, resp):
        """Initializes HttpError with required `response` object.

        1) Constructs the exception message based on the contents of the
        response parameter.

        2) If possible, initializes a 'her_wrap' member which is a
        pypowervm.wrappers.http_error.HttpError - an EntryWrapper for the
        <HttpErrorResponse/> payload.  Consumers should check this member for
        None before using.

        :param resp: pypowervm.adapter.Response containing an
                     <HttpErrorResponse/> response from the REST server.
        """
        self.her_wrap = None
        reason = resp.reason
        # Attempt to extract PowerVM API's HttpErrorResponse object.
        # Since this is an exception path, we use best effort only - don't want
        # problems here to obscure the real exception.
        try:
            root = etree.fromstring(resp.body)
            if root is not None and root.tag == str(etree.QName(c.ATOM_NS,
                                                                'entry')):
                resp.entry = ent.Entry.unmarshal_atom_entry(root, resp)
                # Import inline to avoid circular dependencies
                import pypowervm.wrappers.http_error as he
                self.her_wrap = he.HttpError.wrap(resp)

                # Add the message to the reason if it is available.
                if self.her_wrap.message:
                    reason += ' -- ' + self.her_wrap.message
        except Exception:
            pass
        # Construct the exception message
        msg = ('HTTP error %(status)s for method %(method)s on path '
               '%(path)s: %(reason)s') % dict(status=resp.status,
                                              method=resp.reqmethod,
                                              path=resp.reqpath,
                                              reason=reason)
        # Initialize the exception
        super(HttpError, self).__init__(msg, response=resp)

{'fraction_non_alphanumeric': 0.07746478873239436, 'fraction_numerical': 0.0050704225352112674, 'mean_word_length': 2.1928956834532376, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3184100', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1732, 'n_words': 678}