ai_lib/error_handling/
context.rs

1//! Error context and suggested actions
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Context information for error tracking
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct ErrorContext {
9    /// AI provider that generated the error
10    pub provider: String,
11    /// API endpoint that was called
12    pub endpoint: String,
13    /// Request ID for tracking
14    pub request_id: Option<String>,
15    /// Timestamp when the error occurred
16    pub timestamp: DateTime<Utc>,
17    /// Number of retry attempts
18    pub retry_count: u32,
19    /// Suggested action to take
20    pub suggested_action: SuggestedAction,
21}
22
23/// Suggested actions for error recovery
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub enum SuggestedAction {
26    /// Retry the request with specified delay and max attempts
27    Retry { delay_ms: u64, max_attempts: u32 },
28    /// Switch to alternative providers
29    SwitchProvider { alternative_providers: Vec<String> },
30    /// Reduce request size or complexity
31    ReduceRequestSize { max_tokens: Option<u32> },
32    /// Check API credentials
33    CheckCredentials,
34    /// Contact support with specific reason
35    ContactSupport { reason: String },
36    /// No specific action recommended
37    NoAction,
38}
39
40impl ErrorContext {
41    /// Create a new error context
42    pub fn new(provider: String, endpoint: String) -> Self {
43        Self {
44            provider,
45            endpoint,
46            request_id: None,
47            timestamp: Utc::now(),
48            retry_count: 0,
49            suggested_action: SuggestedAction::NoAction,
50        }
51    }
52
53    /// Create error context with retry information
54    pub fn with_retry(mut self, retry_count: u32) -> Self {
55        self.retry_count = retry_count;
56        self
57    }
58
59    /// Create error context with request ID
60    pub fn with_request_id(mut self, request_id: String) -> Self {
61        self.request_id = Some(request_id);
62        self
63    }
64}