Skip to main content

lc_providers/
error.rs

1// lc-providers/src/error.rs
2//! Unified error type for the lc-providers crate.
3//!
4//! Aggregates all provider-specific error types so the `?` operator works
5//! seamlessly across provider boundaries.
6
7pub use crate::ollama::OllamaError;
8pub use crate::openai::responses::types::ResponsesError;
9pub use crate::openai::AssistantError;
10pub use crate::openai::OpenAIError;
11pub use crate::providers::anthropic::error::AnthropicError;
12pub use crate::providers::gemini::GeminiError;
13
14/// Unified error type that aggregates all LLM provider errors.
15///
16/// This allows using `?` across provider boundaries without manually
17/// mapping error types. Each variant wraps the original provider
18/// error, preserving full context.
19#[derive(Debug)]
20pub enum ProviderError {
21    /// OpenAI API error.
22    OpenAI(OpenAIError),
23    /// Anthropic API error.
24    Anthropic(AnthropicError),
25    /// Gemini API error.
26    Gemini(GeminiError),
27    /// Ollama API error.
28    Ollama(OllamaError),
29    /// OpenAI Assistants API error.
30    Assistant(AssistantError),
31    /// OpenAI Responses API error.
32    Responses(ResponsesError),
33}
34
35impl std::fmt::Display for ProviderError {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            ProviderError::OpenAI(e) => write!(f, "OpenAI error: {e}"),
39            ProviderError::Anthropic(e) => write!(f, "Anthropic error: {e}"),
40            ProviderError::Gemini(e) => write!(f, "Gemini error: {e}"),
41            ProviderError::Ollama(e) => write!(f, "Ollama error: {e}"),
42            ProviderError::Assistant(e) => write!(f, "Assistant error: {e}"),
43            ProviderError::Responses(e) => write!(f, "Responses error: {e}"),
44        }
45    }
46}
47
48impl std::error::Error for ProviderError {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            ProviderError::OpenAI(e) => Some(e),
52            ProviderError::Anthropic(e) => Some(e),
53            ProviderError::Gemini(e) => Some(e),
54            ProviderError::Ollama(e) => Some(e),
55            ProviderError::Assistant(e) => Some(e),
56            ProviderError::Responses(e) => Some(e),
57        }
58    }
59}
60
61// ---- From impls for all provider error types ----
62
63impl From<OpenAIError> for ProviderError {
64    fn from(e: OpenAIError) -> Self {
65        ProviderError::OpenAI(e)
66    }
67}
68impl From<AnthropicError> for ProviderError {
69    fn from(e: AnthropicError) -> Self {
70        ProviderError::Anthropic(e)
71    }
72}
73impl From<GeminiError> for ProviderError {
74    fn from(e: GeminiError) -> Self {
75        ProviderError::Gemini(e)
76    }
77}
78impl From<OllamaError> for ProviderError {
79    fn from(e: OllamaError) -> Self {
80        ProviderError::Ollama(e)
81    }
82}
83impl From<AssistantError> for ProviderError {
84    fn from(e: AssistantError) -> Self {
85        ProviderError::Assistant(e)
86    }
87}
88impl From<ResponsesError> for ProviderError {
89    fn from(e: ResponsesError) -> Self {
90        ProviderError::Responses(e)
91    }
92}