Error Handling
Learn how to handle errors, implement retry strategies, and build resilient applications with the Claro Python SDK.Exception Hierarchy
The SDK provides a hierarchy of exceptions for different error types:BaytAPIError, allowing you to catch all SDK errors with a single handler.
Exception Types
BaytAuthError
Authentication and authorization errors (HTTP 401, 403):- Invalid or expired API key
- Insufficient permissions
- Workspace access denied
BaytNotFoundError
Resource not found errors (HTTP 404):- Incorrect package name
- Wrong version number
- Prompt deleted or moved
- No access to the workspace
BaytRateLimitError
Rate limit exceeded (HTTP 429):BaytValidationError
Invalid request parameters (HTTP 400):- Invalid package name format
- Out of range parameters (e.g., limit > 100)
- Missing required fields
BaytAPIError
Base exception for all API errors:- Network errors
- Server errors (5xx)
- Timeout errors
- All specific exceptions above
Basic Error Handling
Catch Specific Errors
Handle different error types appropriately:Catch All SDK Errors
Use the base exception to catch all SDK errors:Retry Strategies
Built-in Retries
The SDK automatically retries on rate limits and server errors:- 429 (Rate Limit) - Respects
Retry-Afterheader - 5xx (Server Errors) - Uses exponential backoff
- 4xx (Client Errors) - Except 429
- Network Errors
Manual Retry Logic
Implement custom retry logic for specific operations:Retry with Decorators
Use a decorator for reusable retry logic:Graceful Degradation
Provide fallbacks when operations fail:Logging Errors
Integrate with Python’s logging system:Error Context
Provide helpful error messages to users:Complete Example
Best Practices
Catch Specific Exceptions First
Catch Specific Exceptions First
Order exception handlers from most specific to most general:
Don't Retry on Client Errors
Don't Retry on Client Errors
Only retry on transient errors:
Log Errors for Debugging
Log Errors for Debugging
Always log errors with context:
Provide User-Friendly Messages
Provide User-Friendly Messages
Don’t expose technical errors to end users: