from typing import Any, Optional


class HBankError(Exception):
    """The API answered with an error.

    ``code`` is the machine-readable error (``"invalid_amount"``, ``"transaction_not_found"``, ...),
    ``status`` the HTTP status, ``message`` the readable text some errors carry.
    """

    def __init__(self, code: str, status: Optional[int], message: Optional[str] = None, body: Any = None):
        super().__init__(f"{code} (HTTP {status})" + (f": {message}" if message else "") if status else code)
        self.code = code
        self.status = status
        self.message = message
        self.body = body


class AuthenticationError(HBankError):
    """401 ``missing_api_key`` or 403 ``invalid_api_key``: the key is missing, wrong or revoked, or the business is inactive."""


class HBankConnectionError(HBankError):
    """HBank couldn't be reached, or the request timed out. ``status`` is None.

    The request may or may not have reached HBank. Before retrying a refund, check ``refunded`` with ``get_payment``.
    """
