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

CodeMeaningHow to Handle
400Bad RequestFix request parameters
401UnauthorizedRe-authenticate
403Insufficient creditsPurchase more credits
404Not FoundCheck resource ID
429Rate LimitedWait and retry
500Server ErrorRetry 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")