Error Handling Guide
Proper error handling is crucial for building reliable applications. This guide covers all error scenarios you might encounter when using API Codex APIs and how to handle them effectively.
Error Response Format
All API Codex APIs return consistent error responses:
Code
HTTP Status Codes
Success Codes (2xx)
| Code | Status | Description |
|---|---|---|
| 200 | OK | Request successful |
| 201 | Created | Resource created successfully |
| 202 | Accepted | Request accepted for processing |
| 204 | No Content | Request successful, no content to return |
Client Error Codes (4xx)
| Code | Status | Description | Common Causes |
|---|---|---|---|
| 400 | Bad Request | Invalid request parameters | Missing required fields, invalid data types |
| 401 | Unauthorized | Authentication failed | Invalid or missing API key |
| 403 | Forbidden | Access denied | Subscription expired, insufficient permissions |
| 404 | Not Found | Resource not found | Invalid endpoint, deleted resource |
| 405 | Method Not Allowed | HTTP method not supported | Using POST on GET-only endpoint |
| 422 | Unprocessable Entity | Request validation failed | Invalid email format, domain doesn't exist |
| 429 | Too Many Requests | Rate limit exceeded | Too many requests in time window |
Server Error Codes (5xx)
| Code | Status | Description | Action Required |
|---|---|---|---|
| 500 | Internal Server Error | Server-side error | Retry with exponential backoff |
| 502 | Bad Gateway | Invalid upstream response | Retry after brief delay |
| 503 | Service Unavailable | Service temporarily down | Check status page, retry later |
| 504 | Gateway Timeout | Request timeout | Retry with smaller payload |
Common Error Scenarios
Authentication Errors
Invalid API Key
Code
Solution: Verify your API key is correct (no extra spaces), check if the key is active in RapidAPI dashboard, and ensure your subscription is active.
Expired Subscription
Code
Solution: Renew your subscription at RapidAPI. Consider implementing fallback to cached data when subscription issues occur.
Validation Errors
Missing Required Parameters
Code
Solution: Always validate required parameters before making API calls. Check the API documentation for required fields.
Invalid Data Format
Code
Solution: Validate data format before making API calls:
- Email: Use regex
^[^\s@]+@[^\s@]+\.[^\s@]+$ - Domain: Ensure it's a valid domain format
- IP Address: Validate IPv4 or IPv6 format
Rate Limiting Errors
See our comprehensive Rate Limiting Guide for detailed handling strategies.
Implementing Error Handling
A robust error handler should:
- Retry on transient errors (5xx, 429) with exponential backoff
- Fail fast on client errors (4xx except 429)
- Log all errors for debugging and monitoring
- Parse error responses to provide meaningful messages
Code
Error Recovery Strategies
1. Graceful Degradation
When an API call fails, fall back gracefully:
- Return cached data if available
- Return default/fallback data for non-critical features
- Show user-friendly error instead of crashing
2. Circuit Breaker Pattern
Prevent cascading failures by "opening the circuit" after repeated failures:
- CLOSED: Normal operation, requests pass through
- OPEN: Too many failures, reject requests immediately for a timeout period
- HALF-OPEN: After timeout, allow one test request to determine recovery
3. Retry with Jitter
Add randomness to retry delays to prevent thundering herd:
Code
Error Monitoring
For production applications, integrate with error monitoring services:
- Sentry - Real-time error tracking
- LogRocket - Session replay with errors
- DataDog - APM and error monitoring
- Custom webhooks - Send errors to Slack, PagerDuty, etc.
Track metrics like:
- Total errors by status code
- Error rate over time
- Most frequent error types
- Time to detection and resolution
Best Practices
Do's ✅
- Always validate input before making API calls
- Implement retry logic with exponential backoff
- Log all errors for debugging and monitoring
- Use circuit breakers to prevent cascade failures
- Provide meaningful error messages to users
- Cache successful responses for fallback
- Monitor error rates and set up alerts
Don'ts ❌
- Don't ignore errors - Always handle them
- Don't retry infinitely - Set reasonable limits
- Don't expose sensitive information in error messages
- Don't retry non-retryable errors (4xx except 429)
- Don't break on non-critical errors - Degrade gracefully
Next Steps
- Review Rate Limiting for handling 429 errors
- Learn Authentication best practices
- Explore Best Practices for production
- Browse our API Catalog to start building