| 1 | from typing import Any, Optional |
| 2 | |
| 3 | |
| 4 | class HBankError(Exception): |
| 5 | """The API answered with an error. |
| 6 | |
| 7 | ``code`` is the machine-readable error (``"invalid_amount"``, ``"transaction_not_found"``, ...), |
| 8 | ``status`` the HTTP status, ``message`` the readable text some errors carry. |
| 9 | """ |
| 10 | |
| 11 | def __init__(self, code: str, status: Optional[int], message: Optional[str] = None, body: Any = None): |
| 12 | super().__init__(f"{code} (HTTP {status})" + (f": {message}" if message else "") if status else code) |
| 13 | self.code = code |
| 14 | self.status = status |
| 15 | self.message = message |
| 16 | self.body = body |
| 17 | |
| 18 | |
| 19 | class AuthenticationError(HBankError): |
| 20 | """401 ``missing_api_key`` or 403 ``invalid_api_key``: the key is missing, wrong or revoked, or the business is inactive.""" |
| 21 | |
| 22 | |
| 23 | class HBankConnectionError(HBankError): |
| 24 | """HBank couldn't be reached, or the request timed out. ``status`` is None. |
| 25 | |
| 26 | The request may or may not have reached HBank. Before retrying a refund, check ``refunded`` with ``get_payment``. |
| 27 | """ |