Skip to main content

lc_providers/openai/chat/
error.rs

1// lc-providers/src/openai/chat/error.rs
2//! Error types for the OpenAI chat provider.
3
4use crate::ProviderError;
5
6/// OpenAI error type
7#[derive(Debug)]
8#[non_exhaustive]
9pub enum OpenAIError {
10    /// HTTP request error
11    Http(String),
12    /// API-returned error
13    Api(String),
14    /// Response parse error
15    Parse(String),
16    /// Configuration error (missing/malformed environment variables, etc.).
17    Config(String),
18}
19
20impl std::fmt::Display for OpenAIError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            OpenAIError::Http(msg) => write!(f, "HTTP error: {}", msg),
24            OpenAIError::Api(msg) => write!(f, "API error: {}", msg),
25            OpenAIError::Parse(msg) => write!(f, "Parse error: {}", msg),
26            OpenAIError::Config(msg) => write!(f, "Configuration error: {}", msg),
27        }
28    }
29}
30
31impl std::error::Error for OpenAIError {}
32
33impl From<String> for OpenAIError {
34    fn from(s: String) -> Self {
35        OpenAIError::Api(s)
36    }
37}
38
39impl From<ProviderError> for OpenAIError {
40    fn from(e: ProviderError) -> Self {
41        OpenAIError::Config(e.to_string())
42    }
43}