Error Handling
Learn how to handle errors gracefully in your integration.
Error Response Format
All errors follow a consistent format:
{
"error": {
"code": "ERROR_CODE",
"message": "Human-readable error message"
}
}
Common Error Codes
Authentication Errors (401)
| Code | Description | Solution |
|---|
| AUTHENTICATION_FAILED | Authentication failed | Check your API key format |
| INVALID_API_KEY | The provided API key is invalid | Generate a new API key |
| EXPIRED_API_KEY | The API key has expired | Generate a new API key |
| REVOKED_API_KEY | The API key has been revoked | Generate a new API key |
Authorization Errors (403)
| Code | Description | Solution |
|---|
| AUTHORIZATION_FAILED | You do not have permission to access this resource | Check your API key permissions |
| SUSPENDED_ACCOUNT | The client account has been suspended | Contact support |
| INSUFFICIENT_PERMISSIONS | The API key does not have the required permissions | Use an API key with appropriate permissions |
Request Errors (400)
| Code | Description | Solution |
|---|
| INVALID_REQUEST | The request is malformed or contains invalid parameters | Check request body format |
| MISSING_PARAMETER | A required parameter is missing | Include all required fields |
| INVALID_PARAMETER | A parameter has an invalid value | Verify parameter values |
| INVALID_FORMAT | The request format is invalid | Check Content-Type and JSON syntax |
| INSUFFICIENT_BALANCE | Account balance is below the minimum required | Add funds to your account |
Not Found Errors (404)
| Code | Description | Solution |
|---|
| RESOURCE_NOT_FOUND | The requested resource was not found | Verify the resource ID |
| JOB_NOT_FOUND | The specified job was not found | Check the job ID |
| CLIENT_NOT_FOUND | The specified client was not found | Verify client ID |
| API_KEY_NOT_FOUND | The specified API key was not found | Check the key ID |
Rate Limiting (429)
| Code | Description | Solution |
|---|
| RATE_LIMIT_EXCEEDED | Rate limit exceeded | Wait and retry with exponential backoff |
Server Errors (500)
| Code | Description | Solution |
|---|
| INTERNAL_ERROR | An internal server error occurred | Retry with exponential backoff |
| PROCESSING_FAILED | Job processing failed | Check job details for error information |
| DATABASE_ERROR | A database error occurred | Retry after a delay |
| EXTERNAL_SERVICE_ERROR | An external service error occurred | Retry after a delay |
Service Unavailable (503)
| Code | Description | Solution |
|---|
| SERVICE_UNAVAILABLE | The service is temporarily unavailable | Retry after a delay |
Retry Strategy
Implement exponential backoff for transient errors:
Python
import time
import requests
def make_request_with_retry(url, headers, data, max_retries=3):
for attempt in range(max_retries):
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()
elif response.status_code in [429, 500, 503]:
# Retry with exponential backoff
wait_time = 2 ** attempt
time.sleep(wait_time)
else:
# Don't retry client errors
response.raise_for_status()
except requests.exceptions.RequestException as e:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)
raise Exception("Max retries exceeded")
JavaScript
async function makeRequestWithRetry(url, options, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await fetch(url, options);
if (response.ok) {
return await response.json();
}
if ([429, 500, 503].includes(response.status)) {
// Retry with exponential backoff
const waitTime = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, waitTime));
} else {
// Don't retry client errors
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
} catch (error) {
if (attempt === maxRetries - 1) {
throw error;
}
const waitTime = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, waitTime));
}
}
throw new Error('Max retries exceeded');
}
Best Practices
- Always check HTTP status codes before parsing response
- Implement retry logic for 429, 500, and 503 errors
- Log errors with context for debugging
- Handle rate limits gracefully with exponential backoff
- Validate inputs before making API calls
- Monitor error rates to detect issues early