Back to Documentation
Error Handling
Learn how to handle errors and interpret error responses from the Memphora API.
Error Response Format
All error responses follow a consistent format:
{
"error": "Error type",
"detail": "Human-readable error message",
"request_id": "unique-request-id",
"details": [
{
"field": "field_name",
"message": "Specific validation error"
}
]
}error: High-level error category
detail: Human-readable error message
request_id: Unique identifier for tracking the request (useful for support)
details: Array of specific validation errors (for 400/422 errors)
HTTP Status Codes
Best Practices
- Always check the status code before processing the response body
- Log the request_id for debugging and support requests
- Handle rate limits by implementing exponential backoff and respecting retry_after headers
- Validate input before sending requests to avoid 400/422 errors
- Implement retry logic for transient errors (500, 503) with exponential backoff
- Show user-friendly messages based on error details, not raw error codes
Example: Error Handling in Python
import requests
from requests.exceptions import RequestException
def handle_api_call(url, headers, data=None):
try:
response = requests.post(url, json=data, headers=headers)
# Check for errors
if response.status_code == 400:
error_data = response.json()
print(f"Bad Request: {error_data.get('detail')}")
if 'details' in error_data:
for detail in error_data['details']:
print(f" - {detail['field']}: {detail['message']}")
return None
elif response.status_code == 401:
print("Authentication failed. Check your API key.")
return None
elif response.status_code == 429:
retry_after = response.headers.get('Retry-After', 60)
print(f"Rate limit exceeded. Retry after {retry_after} seconds.")
return None
elif response.status_code >= 500:
error_data = response.json()
request_id = error_data.get('request_id')
print(f"Server error (request_id: {request_id}). Please try again later.")
return None
# Success
response.raise_for_status()
return response.json()
except RequestException as e:
print(f"Network error: {e}")
return None