This guide explains how to handle errors when using the Holded API Wrapper.
The Holded API Wrapper provides a hierarchy of exception classes to help you handle different types of errors:
HoldedError: Base exception class for all Holded API errorsHoldedAPIError: Base class for API-related errorsHoldedAuthError: Authentication errors (401)HoldedNotFoundError: Resource not found errors (404)HoldedValidationError: Validation errors (422)HoldedRateLimitError: Rate limit exceeded errors (429)HoldedServerError: Server errors (500+)
HoldedConnectionError: Connection errorsHoldedTimeoutError: Request timeout errors
Here's a basic example of how to handle errors:
from holded.client import HoldedClient
from holded.exceptions import (
HoldedAuthError, HoldedNotFoundError, HoldedValidationError,
HoldedRateLimitError, HoldedServerError, HoldedError
)
client = HoldedClient(api_key="your_api_key")
try:
contact = client.contacts.get("contact_id")
except HoldedAuthError:
print("Authentication failed. Check your API key.")
except HoldedNotFoundError:
print("Contact not found.")
except HoldedValidationError as e:
print(f"Validation error: {e.message}")
except HoldedRateLimitError:
print("Rate limit exceeded. Please try again later.")
except HoldedServerError:
print("Holded server error. Please try again later.")
except HoldedError as e:
print(f"An error occurred: {e.message}")All exception classes provide access to the error details:
try:
contact = client.contacts.get("contact_id")
except HoldedError as e:
print(f"Error message: {e.message}")
print(f"Status code: {e.status_code}")
print(f"Error data: {e.error_data}")The Holded API may have rate limits. You can implement retry logic to handle rate limit errors:
import time
from holded.exceptions import HoldedRateLimitError
max_retries = 3
retry_delay = 1
for attempt in range(max_retries):
try:
contact = client.contacts.get("contact_id")
break # Success, exit the loop
except HoldedRateLimitError:
if attempt < max_retries - 1:
# Wait longer for each retry
wait_time = retry_delay * (attempt + 1)
print(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
time.sleep(wait_time)
else:
print("Rate limit exceeded. Maximum retries reached.")
raiseWhen using the asynchronous client, error handling works the same way:
import asyncio
from holded.async_client import AsyncHoldedClient
from holded.exceptions import HoldedError
async def main():
client = AsyncHoldedClient(api_key="your_api_key")
try:
contact = await client.contacts.get("contact_id")
except HoldedError as e:
print(f"An error occurred: {e.message}")
finally:
await client.close()
asyncio.run(main())- Always handle exceptions: Wrap API calls in try-except blocks to handle errors gracefully.
- Be specific: Catch specific exceptions first, then more general ones.
- Log errors: Log error details for debugging.
- Implement retries: Use retry logic for transient errors like rate limits or server errors.
- Close the client: Always close the client in a finally block to release resources.