Skip to main content

litellm_rust/
error.rs

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