Raised when API key authentication fails (HTTP 401):
from avala import Client, AuthenticationErrortry: client = Client(api_key="invalid_key") projects = client.projects.list()except AuthenticationError as e: print(f"Authentication failed: {e.message}") # Action: Check your API key or regenerate it
Common causes: Invalid API key, expired API key, or missing API key. Verify your credentials in the Avala dashboard.
Raised when a requested resource doesn’t exist (HTTP 404):
from avala import Client, NotFoundErrorclient = Client(api_key="your_api_key")try: project = client.projects.get(id="proj_nonexistent")except NotFoundError as e: print(f"Project not found: {e.message}") # Action: Verify the ID or check if the resource was deleted
Always validate resource IDs before making API calls, especially when accepting user input.
Raised when you exceed the API rate limit (HTTP 429) as defined in avala/errors.py:26-37:
from avala import Client, RateLimitErrorimport timeclient = Client(api_key="your_api_key")try: for i in range(1000): client.tasks.list(project_id="proj_123")except RateLimitError as e: print(f"Rate limit exceeded: {e.message}") print(f"Retry after: {e.retry_after} seconds") if e.retry_after: time.sleep(e.retry_after) # Retry the request
Handle multiple error types in a single try-except block:
from avala import ( Client, AuthenticationError, NotFoundError, RateLimitError, ValidationError, ServerError, AvalaError)client = Client(api_key="your_api_key")try: project = client.projects.get(id="proj_123") tasks = client.tasks.list(project_id=project.id)except AuthenticationError as e: print("Authentication failed. Check your API key.") # Redirect to login or refresh credentialsexcept NotFoundError as e: print(f"Resource not found: {e.message}") # Show a 404 page or create the resourceexcept RateLimitError as e: print(f"Rate limit exceeded. Retry after {e.retry_after}s") # Implement retry logic with backoffexcept ValidationError as e: print(f"Invalid request: {e.message}") for detail in e.details: print(f" - {detail}") # Show validation errors to the userexcept ServerError as e: print(f"Server error: {e.message}") # Log the error and retry or show a maintenance pageexcept AvalaError as e: # Catch any other Avala errors print(f"Unexpected error: {e.message}") # Log and handle gracefully
Implement robust retry logic for transient errors:
import timefrom avala import Client, RateLimitError, ServerError, AvalaErrordef retry_with_backoff(func, max_attempts=5): """Retry a function with exponential backoff.""" for attempt in range(max_attempts): try: return func() except RateLimitError as e: if e.retry_after: wait_time = e.retry_after else: wait_time = 2 ** attempt # Exponential backoff print(f"Rate limited. Waiting {wait_time}s...") time.sleep(wait_time) except ServerError as e: if attempt == max_attempts - 1: raise wait_time = 2 ** attempt print(f"Server error. Retrying in {wait_time}s...") time.sleep(wait_time) except AvalaError: # Don't retry other errors raise raise Exception("Max retry attempts exceeded")client = Client(api_key="your_api_key")# Use the retry wrapperprojects = retry_with_backoff( lambda: client.projects.list())
The SDK automatically retries network errors and server errors (5xx) up to max_retries times. You only need custom retry logic for rate limiting or specific use cases.