import logging
from functools import wraps

from .exceptions import (
    ArchivingOperationCustomException,
    CertificateNotValidCustomException,
    RecordCanNotBeInsertedCustomException,
    WrongTaskCustomException,
)

log = logging.getLogger(__name__)

def task_exceptions_wrapper(function):
    """
    Catches exceptions after which task should be acknowledged
    and not repeated since there is not issue with worker
    but with input values
    """

    @wraps(function)
    def wrapper(*args, **kwargs):
        try:
            return function(*args, **kwargs)
        except CertificateNotValidCustomException:  # as e: - was unused
            log.exception(
                "Certificate is invalid",
                exc_info=True,
                stack_info=True,
            )
            result = "FAILED"
        except (ArchivingOperationCustomException):  # as e: - was unused
            log.exception(
                "Exception occured during archiving, verification or"
                " timestamping process",
                exc_info=True,
                stack_info=True,
            )
            result = "FAILED"
        except (WrongTaskCustomException):  # as e: - was unused
            log.exception(
                "Wrong task came for given worker",
                exc_info=True,
                stack_info=True,
            )
            result = "KNOWN_ERROR"
        except (RecordCanNotBeInsertedCustomException):  # as e: - was unused
            log.exception(
                "Constraint error",
                exc_info=True,
                stack_info=True,
            )
            result = "FAILED"
        except (EOFError):  # as e: - was unused
            log.exception(
                "File operation error",
                exc_info=True,
                stack_info=True,
            )
            result = "FAILED"
        return result

    return wrapper
