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::azure::AzureOpenAIError;
13pub use crate::providers::cohere::CohereError;
14pub use crate::providers::gemini::GeminiError;
15
16/// Unified error type that aggregates all LLM provider errors.
17///
18/// This allows using `?` across provider boundaries without manually
19/// mapping error types. Each variant wraps the original provider
20/// error, preserving full context.
21#[derive(Debug)]
22pub enum ProviderError {
23    /// OpenAI API error.
24    OpenAI(OpenAIError),
25    /// Anthropic API error.
26    Anthropic(AnthropicError),
27    /// Gemini API error.
28    Gemini(GeminiError),
29    /// Azure OpenAI API error.
30    Azure(AzureOpenAIError),
31    /// Cohere API error.
32    Cohere(CohereError),
33    /// Ollama API error.
34    Ollama(OllamaError),
35    /// OpenAI Assistants API error.
36    Assistant(AssistantError),
37    /// OpenAI Responses API error.
38    Responses(ResponsesError),
39}
40
41impl std::fmt::Display for ProviderError {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            ProviderError::OpenAI(e) => write!(f, "OpenAI error: {e}"),
45            ProviderError::Anthropic(e) => write!(f, "Anthropic error: {e}"),
46            ProviderError::Gemini(e) => write!(f, "Gemini error: {e}"),
47            ProviderError::Azure(e) => write!(f, "Azure OpenAI error: {e}"),
48            ProviderError::Cohere(e) => write!(f, "Cohere error: {e}"),
49            ProviderError::Ollama(e) => write!(f, "Ollama error: {e}"),
50            ProviderError::Assistant(e) => write!(f, "Assistant error: {e}"),
51            ProviderError::Responses(e) => write!(f, "Responses error: {e}"),
52        }
53    }
54}
55
56impl std::error::Error for ProviderError {
57    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
58        match self {
59            ProviderError::OpenAI(e) => Some(e),
60            ProviderError::Anthropic(e) => Some(e),
61            ProviderError::Gemini(e) => Some(e),
62            ProviderError::Azure(e) => Some(e),
63            ProviderError::Cohere(e) => Some(e),
64            ProviderError::Ollama(e) => Some(e),
65            ProviderError::Assistant(e) => Some(e),
66            ProviderError::Responses(e) => Some(e),
67        }
68    }
69}
70
71// ---- From impls for all provider error types ----
72
73impl From<OpenAIError> for ProviderError {
74    fn from(e: OpenAIError) -> Self {
75        ProviderError::OpenAI(e)
76    }
77}
78impl From<AnthropicError> for ProviderError {
79    fn from(e: AnthropicError) -> Self {
80        ProviderError::Anthropic(e)
81    }
82}
83impl From<GeminiError> for ProviderError {
84    fn from(e: GeminiError) -> Self {
85        ProviderError::Gemini(e)
86    }
87}
88impl From<OllamaError> for ProviderError {
89    fn from(e: OllamaError) -> Self {
90        ProviderError::Ollama(e)
91    }
92}
93impl From<AssistantError> for ProviderError {
94    fn from(e: AssistantError) -> Self {
95        ProviderError::Assistant(e)
96    }
97}
98impl From<ResponsesError> for ProviderError {
99    fn from(e: ResponsesError) -> Self {
100        ProviderError::Responses(e)
101    }
102}
103impl From<AzureOpenAIError> for ProviderError {
104    fn from(e: AzureOpenAIError) -> Self {
105        ProviderError::Azure(e)
106    }
107}
108impl From<CohereError> for ProviderError {
109    fn from(e: CohereError) -> Self {
110        ProviderError::Cohere(e)
111    }
112}
113
114// ---- LCEL Error conversion ----
115
116/// Allow `ProviderError` to convert into `LcelError` for LCEL pipeline compatibility.
117/// This enables LLM providers to participate in `pipe()` chains.
118impl From<ProviderError> for lc_core::LcelError {
119    fn from(err: ProviderError) -> Self {
120        lc_core::LcelError::Provider(err.to_string())
121    }
122}