Skip to main content

litellm_rs/
error.rs

1use std::error::Error as StdError;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum LiteLLMError {
6    #[error("configuration error: {0}")]
7    Config(String),
8    #[error("provider not found: {0}")]
9    ProviderNotFound(String),
10    #[error("missing api key for provider: {0}")]
11    MissingApiKey(String),
12    #[error("http error: {message}")]
13    Http {
14        message: String,
15        #[source]
16        source: Option<Box<dyn StdError + Send + Sync>>,
17    },
18    #[error("parse error: {0}")]
19    Parse(String),
20    #[error("unsupported operation: {0}")]
21    Unsupported(String),
22}
23
24pub type Result<T> = std::result::Result<T, LiteLLMError>;
25
26impl LiteLLMError {
27    pub fn http(message: impl Into<String>) -> Self {
28        Self::Http {
29            message: message.into(),
30            source: None,
31        }
32    }
33
34    pub fn http_with_source<E>(err: E) -> Self
35    where
36        E: StdError + Send + Sync + 'static,
37    {
38        Self::Http {
39            message: err.to_string(),
40            source: Some(Box::new(err)),
41        }
42    }
43}
44
45impl From<reqwest::Error> for LiteLLMError {
46    fn from(err: reqwest::Error) -> Self {
47        LiteLLMError::http_with_source(err)
48    }
49}
50
51impl From<std::io::Error> for LiteLLMError {
52    fn from(err: std::io::Error) -> Self {
53        LiteLLMError::http_with_source(err)
54    }
55}