Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2015 TrilioData, Inc.
# All Rights Reserved.

"""Contego base exception handling.

"""

from contego.i18n import _, _LE

try:
    from oslo_log import log as logging
except ImportError:
    from .nova.openstack.common import log as logging

import functools
from oslo_utils import excutils

LOG = logging.getLogger(__name__)

def wrap_exception( ):
    """This decorator wraps a method to catch any exceptions that may
    get thrown. It also optionally sends the exception to the notification
    system.
    """
    def inner(f):
        def wrapped(self, context, *args, **kw):
            # Don't store self or context in the payload, it now seems to
            # contain confidential information.
            try:
                return f(self, context, *args, **kw)
            except Exception as exc:
                with excutils.save_and_reraise_exception():
                    function_name = f.__name__
                    LOG.exception("Error occurred: {} when running {} method".format(str(exc), function_name))
        return functools.wraps(f)(wrapped)
    return inner


class ContegoException(Exception):
    """Base Contego Exception

    To correctly use this class, inherit from it and define
    a 'message' property. That message will get printf'd
    with the keyword arguments provided to the constructor.

    """

    message = "Error: %(reason)s"
    code = 500
    headers = {}
    safe = False

    def __init__(self, message=None, **kwargs):
        self.kwargs = kwargs

        if "code" not in self.kwargs:
            try:
                self.kwargs["code"] = self.code
            except AttributeError:
                pass

        if not message:
            try:
                message = self.message % kwargs

            except Exception:
                # kwargs doesn't match a variable in the message
                # log the issue and the kwargs
                LOG.exception("Exception in string format operation")
                for name, value in kwargs.items():
                    LOG.error("%s: %s" % (name, value))

                # at least get the core message out if something happened
                message = self.message

        super(Exception, self).__init__(message)


class ErrorOccurred(ContegoException):
    message = "%(reason)s"


class TimeoutOccurred(ContegoException):
    message = "%(reason)s"


class InvalidNFSMountPoint(ContegoException):
    pass


class RetryException(ContegoException):
    pass


class NotFound(ContegoException):
    message = "Resource could not be found."
    code = 404


class FileNotFound(NotFound):
    message = "File %(file_path)s could not be found."

class ServiceNotFound(NotFound):
    msg_fmt = _("Service %(service_id)s could not be found.")

class ServiceBinaryExists(ContegoException):
    msg_fmt = _("Service with host %(host)s binary %(binary)s exists.")


class ServiceTopicExists(ContegoException):
    msg_fmt = _("Service with host %(host)s topic %(topic)s exists.")


class HostNotFound(NotFound):
    msg_fmt = _("Host %(host)s could not be found.")

class ComputeHostNotFound(HostNotFound):
    msg_fmt = _("Compute host %(host)s could not be found.")

class HostBinaryNotFound(NotFound):
    msg_fmt = _("Could not find binary %(binary)s on host %(host)s.")

class ObjectActionError(ContegoException):
    msg_fmt = _('Object action %(action)s failed because: %(reason)s')

class Invalid(ContegoException):
    msg_fmt = _("Bad Request - Invalid Parameters")
    code = 400

class ServiceTooOld(Invalid):
    msg_fmt = _("This service is older (v%(thisver)i) than the minimum "
                "(v%(minver)i) version of the rest of the deployment. "
                "Unable to continue.")

class InvalidMountPoint(ContegoException):
    pass

class CancelException(ContegoException):
    message = _("User initiated cancel request")