Exception Types
The SDK provides specific exception types for different error scenarios:
import omophub
client = omophub.OMOPHub(api_key="oh_xxx")
try:
concept = client.concepts.get(999999999)
except omophub.NotFoundError as e:
print(f"Not found: {e.message}")
except omophub.AuthenticationError as e:
print(f"Auth failed: {e.message}")
except omophub.RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after}s")
except omophub.ValidationError as e:
print(f"Invalid request: {e.message}")
except omophub.APIError as e:
print(f"API error {e.status_code}: {e.message}")
Exception Hierarchy
OMOPHubError # Base exception for all SDK errors
├── APIError # API returned an error response
│ ├── AuthenticationError # 401 - Invalid or missing API key
│ ├── NotFoundError # 404 - Resource not found
│ ├── ValidationError # 400 - Invalid request parameters
│ ├── RateLimitError # 429 - Too many requests
│ └── ServerError # 500+ - Server-side error
└── ConnectionError # Network connectivity issues
Error Properties
All exceptions include helpful properties:
try:
concept = client.concepts.get(999999999)
except omophub.APIError as e:
print(e.message) # Human-readable error message
print(e.status_code) # HTTP status code
print(e.request_id) # Request ID for debugging
Rate Limiting
Handle rate limits with automatic retry:
try:
results = client.search.basic("diabetes")
except omophub.RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds")
import time
time.sleep(e.retry_after)
# Retry the request
results = client.search.basic("diabetes")
The SDK automatically retries failed requests up to max_retries times (default: 3) with exponential backoff.
Connection Errors
Handle network issues:
try:
concept = client.concepts.get(201826)
except omophub.ConnectionError as e:
print(f"Network error: {e.message}")
# Implement your retry logic or fallback
Best Practices
Catch Specific Exceptions First
try:
concept = client.concepts.get(concept_id)
except omophub.NotFoundError:
# Handle missing concept
concept = None
except omophub.RateLimitError as e:
# Wait and retry
time.sleep(e.retry_after)
except omophub.APIError as e:
# Log and handle other API errors
logger.error(f"API error: {e.message}")
raise
Use Context Manager for Async
async with omophub.AsyncOMOPHub(api_key="oh_xxx") as client:
try:
concept = await client.concepts.get(201826)
except omophub.APIError as e:
print(f"Error: {e.message}")