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 { 
28        delay_ms: u64, 
29        max_attempts: u32 
30    },
31    /// Switch to alternative providers
32    SwitchProvider { 
33        alternative_providers: Vec<String> 
34    },
35    /// Reduce request size or complexity
36    ReduceRequestSize { 
37        max_tokens: Option<u32> 
38    },
39    /// Check API credentials
40    CheckCredentials,
41    /// Contact support with specific reason
42    ContactSupport { 
43        reason: String 
44    },
45    /// No specific action recommended
46    NoAction,
47}
48
49impl ErrorContext {
50    /// Create a new error context
51    pub fn new(provider: String, endpoint: String) -> Self {
52        Self {
53            provider,
54            endpoint,
55            request_id: None,
56            timestamp: Utc::now(),
57            retry_count: 0,
58            suggested_action: SuggestedAction::NoAction,
59        }
60    }
61
62    /// Create error context with retry information
63    pub fn with_retry(mut self, retry_count: u32) -> Self {
64        self.retry_count = retry_count;
65        self
66    }
67
68    /// Create error context with request ID
69    pub fn with_request_id(mut self, request_id: String) -> Self {
70        self.request_id = Some(request_id);
71        self
72    }
73}