Se rendre au contenu

Error Handling

Handle failures gracefully with retries, fallbacks, and proper logging.

Common Errors

Error Cause Solution
RateLimitError Too many requests Implement exponential backoff
InvalidRequestError Bad parameters or prompt Validate input, check token limits
AuthenticationError Invalid API key Verify key configuration
ServiceUnavailable Provider outage Retry with backoff, use fallback
ContextLengthExceeded Prompt too long Reduce prompt size, use larger model

Retry Strategy

Configure automatic retries for transient failures.

Python
provider.write({
    'max_retries': 3,
    'retry_delay': 5,           # Initial delay in seconds
    'retry_backoff': 2,         # Exponential backoff multiplier
    'retry_max_delay': 60,      # Maximum delay between retries
    'retry_on': ['rate_limit', 'server_error'],  # Error types to retry
})

Logging

Enable comprehensive logging for debugging and monitoring.

Python
import logging

_logger = logging.getLogger(__name__)

def process_task(self, task):
    try:
        _logger.info(f"Processing task {task.id} for project {self.id}")
        result = self._call_ai_provider(task)
        _logger.info(f"Task {task.id} completed successfully")
        return result

    except RateLimitError as e:
        _logger.warning(f"Rate limit hit for task {task.id}: {e}")
        raise

    except Exception as e:
        _logger.error(f"Task {task.id} failed: {e}", exc_info=True)
        task.write({
            'ai_state': 'failed',
            'ai_error': str(e),
        })
        raise

User Communication

Provide clear, actionable error messages to users.

User-Friendly Messages

Don't expose technical details to end users. Transform API errors into helpful guidance:

Python
USER_MESSAGES = {
    'rate_limit': "Our AI service is busy. Your request has been queued and will process shortly.",
    'context_length': "Your project description is too long. Please shorten it and try again.",
    'authentication': "There's a configuration issue. Please contact support.",
    'server_error': "The AI service is temporarily unavailable. We'll retry automatically.",
}

def get_user_message(self, error_type):
    return USER_MESSAGES.get(error_type, "An unexpected error occurred. Please try again.")