autoagents_llm/
error.rs

1use std::fmt;
2
3/// Error types that can occur when interacting with LLM providers.
4#[derive(Debug)]
5pub enum LLMError {
6    /// HTTP request/response errors
7    HttpError(String),
8    /// Authentication and authorization errors
9    AuthError(String),
10    /// Invalid request parameters or format
11    InvalidRequest(String),
12    /// Errors returned by the LLM provider
13    ProviderError(String),
14    /// API response parsing or format error
15    ResponseFormatError {
16        message: String,
17        raw_response: String,
18    },
19    /// Generic error
20    Generic(String),
21    /// JSON serialization/deserialization errors
22    JsonError(String),
23    /// Tool configuration error
24    ToolConfigError(String),
25}
26
27impl fmt::Display for LLMError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            LLMError::HttpError(e) => write!(f, "HTTP Error: {}", e),
31            LLMError::AuthError(e) => write!(f, "Auth Error: {}", e),
32            LLMError::InvalidRequest(e) => write!(f, "Invalid Request: {}", e),
33            LLMError::ProviderError(e) => write!(f, "Provider Error: {}", e),
34            LLMError::Generic(e) => write!(f, "Generic Error : {}", e),
35            LLMError::ResponseFormatError {
36                message,
37                raw_response,
38            } => {
39                write!(
40                    f,
41                    "Response Format Error: {}. Raw response: {}",
42                    message, raw_response
43                )
44            }
45            LLMError::JsonError(e) => write!(f, "JSON Parse Error: {}", e),
46            LLMError::ToolConfigError(e) => write!(f, "Tool Configuration Error: {}", e),
47        }
48    }
49}
50
51impl std::error::Error for LLMError {}
52
53/// Converts reqwest HTTP errors into LlmErrors
54impl From<reqwest::Error> for LLMError {
55    fn from(err: reqwest::Error) -> Self {
56        LLMError::HttpError(err.to_string())
57    }
58}
59
60impl From<serde_json::Error> for LLMError {
61    fn from(err: serde_json::Error) -> Self {
62        LLMError::JsonError(format!(
63            "{} at line {} column {}",
64            err,
65            err.line(),
66            err.column()
67        ))
68    }
69}