Error Handling
Comprehensive guide to API error codes, common issues, and troubleshooting strategies. Build robust applications with proper error handling.
Error Categories
Client Errors (4xx)
Errors caused by invalid requests or authentication issues
Server Errors (5xx)
Temporary server-side issues and service unavailability
Rate Limit Errors
Usage limits exceeded, requires backoff and retry
Common Error Codes
Bad Request
Invalid request format, missing parameters, or malformed JSON
Common Causes
Solutions
Unauthorized
Missing, invalid, or expired authentication credentials
Common Causes
Solutions
Forbidden
Valid authentication but insufficient permissions
Common Causes
Solutions
Not Found
Requested endpoint or resource does not exist
Common Causes
Solutions
Payload Too Large
Request body exceeds maximum allowed size
Common Causes
Solutions
Unprocessable Entity
Request is valid but contains semantic errors
Common Causes
Solutions
Too Many Requests
Rate limit exceeded, requests are being throttled
Common Causes
Solutions
Internal Server Error
Unexpected server-side error occurred
Common Causes
Solutions
Bad Gateway
Error from upstream AI provider service
Common Causes
Solutions
Service Unavailable
Service temporarily unavailable or under maintenance
Common Causes
Solutions
Error Response Format
Standard Error Response
{ "error": { "type": "invalid_request", "message": "The prompt parameter is required", "code": 400 }, "request_id": "req_abc123def456", "timestamp": "2024-01-01T12:00:00Z" }
Detailed Error Response
{ "error": { "type": "validation_error", "message": "Parameter validation failed", "code": 422, "details": { "field": "temperature", "issue": "Value 3.5 exceeds maximum of 2.0" } }, "request_id": "req_def456ghi789", "timestamp": "2024-01-01T12:00:00Z" }
Rate Limiting & Retry Logic
Rate Limit Headers
X-RateLimit-Limit: 1000 // Requests per hour X-RateLimit-Remaining: 842 // Remaining requests X-RateLimit-Reset: 1704063600 // Reset timestamp X-RateLimit-Retry-After: 3600 // Seconds until reset
Exponential Backoff Implementation
JavaScript Example
async function makeRequestWithRetry(data, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Authorization': 'Bearer sk-fusion-...', 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (response.status === 429) { const retryAfter = response.headers.get('Retry-After'); const delay = retryAfter ? parseInt(retryAfter) * 1000 : Math.pow(2, i) * 1000; await new Promise(resolve => setTimeout(resolve, delay) ); continue; } return response; } catch (error) { if (i === maxRetries - 1) throw error; await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000) ); } } }
Python Example
import time import requests from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry def create_session_with_retries(): session = requests.Session() retry_strategy = Retry( total=3, status_forcelist=[429, 500, 502, 503, 504], backoff_factor=1 ) adapter = HTTPAdapter(max_retries=retry_strategy) session.mount("http://", adapter) session.mount("https://", adapter) return session # Usage session = create_session_with_retries() response = session.post( 'https://api.mcp4.ai/chat', headers={'Authorization': 'Bearer sk-fusion-...'}, json={'prompt': 'Hello world'} )