1use std::fmt;
2
3#[derive(Debug)]
5pub enum LLMError {
6 HttpError(String),
8 AuthError(String),
10 InvalidRequest(String),
12 ProviderError(String),
14 ResponseFormatError {
16 message: String,
17 raw_response: String,
18 },
19 Generic(String),
21 JsonError(String),
23 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
53impl 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}