Skip to content

errors

errors

Error classification and optional typed exceptions.

API calls never raise by default; they return an error dict. Responses now also carry an error_kind key (alongside the legacy error_type) and, when the API supplied one, reason. Use :func:raise_for_error to convert an error dict into a typed exception.

CocApiError

CocApiError(response: dict[str, Any])

Bases: Exception

Base class for typed API errors.

Source code in cocapi/errors.py
55
56
57
58
59
60
def __init__(self, response: dict[str, Any]):
    self.response = response
    self.status_code = response.get("status_code") or response.get("http_status")
    self.kind = response.get("error_kind")
    self.reason = response.get("reason")
    super().__init__(response.get("message", "API error"))

classify_http_error

classify_http_error(
    status: int,
    body: Any = None,
    endpoint: str | None = None,
) -> tuple[str, str | None]

Return (error_kind, reason) for an HTTP error response.

Source code in cocapi/errors.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def classify_http_error(
    status: int, body: Any = None, endpoint: str | None = None
) -> tuple[str, str | None]:
    """Return ``(error_kind, reason)`` for an HTTP error response."""
    reason = body.get("reason") if isinstance(body, dict) else None
    reason = reason if isinstance(reason, str) else None

    if status == 503 or reason == "inMaintenance":
        return MAINTENANCE, reason
    if status == 404:
        return NOT_FOUND, reason
    if status == 429:
        return RATE_LIMITED, reason
    if status == 400:
        return BAD_REQUEST, reason
    if status == 403:
        if reason == "accessDenied.invalidIp":
            return INVALID_IP, reason
        # A 403 on war endpoints with a valid key means the war log is private.
        if endpoint and ("/currentwar" in endpoint or "/warlog" in endpoint):
            if reason in (None, "accessDenied"):
                return PRIVATE_WAR_LOG, reason
        return ACCESS_DENIED, reason
    if status >= 500:
        return SERVER_ERROR, reason
    return HTTP_ERROR, reason

raise_for_error

raise_for_error(response: Any) -> Any

Raise a typed :class:CocApiError if response is an error dict.

Returns the response unchanged when it is not an error, so it can wrap calls::

clan = raise_for_error(api.clan_tag("#ABC"))
Source code in cocapi/errors.py
 96
 97
 98
 99
100
101
102
103
104
105
106
def raise_for_error(response: Any) -> Any:
    """Raise a typed :class:`CocApiError` if ``response`` is an error dict.

    Returns the response unchanged when it is not an error, so it can wrap calls::

        clan = raise_for_error(api.clan_tag("#ABC"))
    """
    if isinstance(response, dict) and response.get("result") == "error":
        exc = _KIND_TO_EXC.get(response.get("error_kind", ""), CocApiError)
        raise exc(response)
    return response