Source code for contree_client.exceptions
"""Public exceptions raised by the Contree API client."""
from __future__ import annotations
from typing import Any
[docs]
class ContreeError(Exception):
"""Base class for normalized API client errors."""
[docs]
class APIConnectionError(ContreeError):
"""A connection failed or a response body was interrupted.
``timed_out`` identifies a timeout without exposing a backend type.
"""
def __init__(self, message: str, *, timed_out: bool = False) -> None:
super().__init__(message)
self.timed_out = timed_out
[docs]
class APIStatusError(ContreeError):
"""A request or stream received HTTP status 400 or greater."""
def __init__(
self,
status: int,
error: Any,
*,
traceback: list[str] | None = None,
retry_after: int | None = None,
) -> None:
super().__init__(f"HTTP {status}: {error}")
self.status = status
self.error = error
self.traceback = traceback
self.retry_after = retry_after
[docs]
class BadRequestError(APIStatusError):
"""The API returned HTTP 400 Bad Request."""
[docs]
class AuthenticationError(APIStatusError):
"""The API returned HTTP 401 Unauthorized."""
[docs]
class PermissionDeniedError(APIStatusError):
"""The API returned HTTP 403 Forbidden."""
[docs]
class NotFoundError(APIStatusError):
"""The API returned HTTP 404 Not Found."""
[docs]
class ConflictError(APIStatusError):
"""The API returned HTTP 409 Conflict."""
[docs]
class GoneError(APIStatusError):
"""The API returned HTTP 410 Gone."""
[docs]
class UnprocessableEntityError(APIStatusError):
"""The API returned HTTP 422 Unprocessable Entity."""
[docs]
class TooEarlyError(APIStatusError):
"""The API returned HTTP 425 Too Early."""
[docs]
class RateLimitError(APIStatusError):
"""The API returned HTTP 429 Too Many Requests."""
[docs]
class ServerError(APIStatusError):
"""The API returned an HTTP status of 500 or greater."""
ERROR_CLASSES: dict[int, type[APIStatusError]] = {
400: BadRequestError,
401: AuthenticationError,
403: PermissionDeniedError,
404: NotFoundError,
409: ConflictError,
410: GoneError,
422: UnprocessableEntityError,
425: TooEarlyError,
429: RateLimitError,
}