# Copyright (c) Cloud Linux Software, Inc
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT

from .py23 import HTTPError


class SafeExceptionWrapper(Exception):
    def __init__(self, inner, etype=None, details=None):
        self.inner = inner
        self.etype = etype
        self.details = details


class KcareError(Exception):
    """Base kernelcare exception which will be considered as expected
    error and the full traceback will not be shown.

    Subclasses may set a class-level ``status`` to provide a short,
    fixed label for error reporting.  Individual raise sites can
    override it per-instance via the ``status`` kwarg.
    """

    status = ''  # type: str

    def __init__(self, *args, **kwargs):  # type: (*object, **object) -> None
        status = kwargs.pop('status', None)
        if status is not None:
            self.status = str(status)
        super(KcareError, self).__init__(*args)


class NotFound(HTTPError):
    pass


class NoLibcareLicenseException(KcareError):
    status = 'no libcare license'


class CapabilitiesMismatch(KcareError):
    status = 'capabilities mismatch'


class AlreadyTrialedException(KcareError):
    status = 'already trialed'

    def __init__(self, ip, created, *args, **kwargs):
        super(AlreadyTrialedException, self).__init__(*args, **kwargs)
        self.created = created[0 : created.index('T')]
        self.ip = ip

    def __str__(self):
        return 'The IP {0} was already used for a trial license on {1}'.format(self.ip, self.created)


class UnableToGetLicenseException(KcareError):
    status = 'unable to get license'

    def __init__(self, code, **kwargs):
        super(UnableToGetLicenseException, self).__init__(
            'Unknown Issue when getting trial license. Error code: ' + str(code), **kwargs
        )


class BadSignatureException(KcareError):
    status = 'bad signature'


def check_exc(*exc_list):
    def inner(e, state):
        return isinstance(e, exc_list)

    return inner
