import logging
from openstack.exceptions import (
    SDKException, BadRequestException)


class ErrorException(Exception):
    """ Base class for exceptions in this module.

    Attributes:
        OpenStackError -- boolean
        DuplicateError -- boolean
    """

    def __init__(self, e):
        super().__init__(e)

        self.OpenStackError = True
        self.DuplicateError = False
        if hasattr(e, 'details'):
            if 'duplicate' in e.details:
                self.DuplicateError = True


def OpenStackError(func):
    """ Decorator to wrap openstack error in simple Exception.
    Return decorated function
    """

    def wrap_OpenStackError(*args, **kw):

        try:
            try:
                return func(*args, **kw)

            except SDKException as e:
                raise ErrorException(e)

            except BadRequestException as e:
                import pdb; pdb.set_trace()
                raise ErrorException(e)

        except Exception as e:
            raise e

    return wrap_OpenStackError