Guides
Error Handling
Learn how to handle errors from the Twin.Actor API gracefully and provide good user experiences.
Error Response Format
All errors follow this format:
json
{
"detail": "Error message describing what went wrong"
}Validation errors include field details:
json
{
"detail": [
{
"loc": ["body", "email"],
"msg": "Invalid email format",
"type": "value_error"
}
]
}Common Error Codes
| Code | Meaning | How to Handle |
|---|---|---|
400 | Bad Request | Fix request parameters |
401 | Unauthorized | Re-authenticate |
403 | Insufficient credits | Purchase more credits |
404 | Not Found | Check resource ID |
429 | Rate Limited | Wait and retry |
500 | Server Error | Retry later, contact support |
Retry Strategy
For transient errors (500, 502, 503, 504, 429), implement exponential backoff:
python
"text-violet-400">import time
"text-violet-400">import requests
"text-violet-400">def api_request_with_retry(url, max_retries=3):
"text-violet-400">for attempt "text-violet-400">in range(max_retries):
response = requests.get(url, headers=headers)
"text-violet-400">if response.status_code == 429:
# Rate limited - wait "text-violet-400">as specified
wait_time = int(response.headers.get("Retry-After", 60))
time.sleep(wait_time)
continue
"text-violet-400">if response.status_code >= 500:
# Server error - exponential backoff
time.sleep(2 ** attempt)
continue
"text-violet-400">return response
raise Exception("Max retries exceeded")