Error Handling

Handle errors in the Python SDK

Handle errors gracefully in your application.

Error Types

from limitry import Client, AuthenticationError, APIError, NetworkError

try:
    async with Client(api_key="invalid-key") as client:
        await client.project.get()
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
    print(f"Status code: {e.status}")
except APIError as e:
    print(f"API error: {e}")
    print(f"Status: {e.status} {e.reason}")
except NetworkError as e:
    print(f"Network error: {e}")

Retry Logic

import asyncio
from limitry import Client, APIError

async def track_with_retry(client, max_retries=3):
    for attempt in range(max_retries):
        try:
            await client.track.track_usage(...)
            return
        except APIError as e:
            if e.status == 429:  # Rate limited
                await asyncio.sleep(2 ** attempt)  # Exponential backoff
            else:
                raise

Next Steps

On this page