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    /// The SSE stream ended before a terminal marker (`[DONE]`, or a chunk
19    /// carrying `finish_reason`) was observed — i.e. the connection dropped
20    /// mid-generation. A12: the partial text streamed so far is truncated and
21    /// must not be presented as a complete answer.
22    StreamInterrupted(String),
23}
24
25impl std::fmt::Display for OpenAIError {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            OpenAIError::Http(msg) => write!(f, "HTTP error: {}", msg),
29            OpenAIError::Api(msg) => write!(f, "API error: {}", msg),
30            OpenAIError::Parse(msg) => write!(f, "Parse error: {}", msg),
31            OpenAIError::Config(msg) => write!(f, "Configuration error: {}", msg),
32            OpenAIError::StreamInterrupted(msg) => write!(
33                f,
34                "stream interrupted before terminal event (output truncated): {}",
35                msg
36            ),
37        }
38    }
39}
40
41impl std::error::Error for OpenAIError {}
42
43impl From<String> for OpenAIError {
44    fn from(s: String) -> Self {
45        OpenAIError::Api(s)
46    }
47}
48
49impl From<ProviderError> for OpenAIError {
50    fn from(e: ProviderError) -> Self {
51        OpenAIError::Config(e.to_string())
52    }
53}