Python clientError handling

Error handling

The Python clients call response.raise_for_status() internally, so a failed request raises httpx.HTTPStatusError. The response is attached, so you can inspect the status code and body. Successful responses return parsed JSON (a dict or list); a 204 No Content returns None.

Catching errors

import httpx

try:
    pool = resources.get_pool(pool_id)
except httpx.HTTPStatusError as err:
    print(f"Request failed: {err.response.status_code} {err.response.reason_phrase}")
    print("Body:", err.response.text)   # e.g. {"detail": "..."} on a 422
except httpx.RequestError as err:
    print(f"Network error: {err}")       # connection/timeout, no response
  • httpx.HTTPStatusError — the server returned a 4xx/5xx. Use err.response.status_code and err.response.json() / .text.
  • httpx.RequestError — the request never completed (DNS, connection, timeout). The constructor timeout (default 30.0s) governs the latter.

Handling expired tokens

A 401 means the access token is missing or expired. Refresh and retry:

import httpx

def with_auth(call):
    try:
        return call()
    except httpx.HTTPStatusError as err:
        if err.response.status_code == 401:
            tokens = auth.refresh_token()
            resources.set_access_token(tokens["access_token"])
            return call()  # retry once
        raise

pools = with_auth(lambda: resources.get_all_pools())

For long-running processes, call auth.auto_refresh() periodically (it refreshes when the token is near expiry) so requests rarely hit a 401.

Common statuses

StatusMeaningTypical fix
400Bad requestCheck the payload shape
401UnauthenticatedRefresh or re-issue the access token
403ForbiddenThe user lacks permission
404Not foundVerify the id / path
422Validation errorInspect err.response.json()["detail"]