Skip to main content

llmix_rs/
error.rs

1use std::collections::HashMap;
2use thiserror::Error;
3
4pub type LlmixResult<T> = Result<T, LlmixError>;
5
6#[derive(Debug, Clone, PartialEq, Eq, Error)]
7#[error("circuit breaker OPEN for ({provider}, {base_url})")]
8pub struct CircuitOpenError {
9    pub provider: String,
10    pub base_url: String,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq, Error)]
14#[error("kill switch active: {path}")]
15pub struct KillSwitchActiveError {
16    pub path: String,
17}
18
19#[derive(Debug, Clone, PartialEq, Eq, Error)]
20#[error("all {total_keys} keys are dead (401). Replace keys or wait for provider resolution.")]
21pub struct KeyPoolExhaustedError {
22    pub total_keys: usize,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq, Error)]
26#[error("config file not found: {path}")]
27pub struct ConfigNotFoundError {
28    pub path: String,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, Error)]
32#[error("permission denied reading config file: {path}")]
33pub struct ConfigAccessError {
34    pub path: String,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Error)]
38#[error("{message}")]
39pub struct InvalidConfigError {
40    pub message: String,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Error)]
44#[error("{message}")]
45pub struct SecurityError {
46    pub message: String,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
50#[error("adaptive semaphore is closed")]
51pub struct AdaptiveSemaphoreClosedError;
52
53#[derive(Debug, Clone, PartialEq, Eq, Error)]
54#[error("{message}")]
55pub struct ProviderError {
56    pub message: String,
57    pub status_code: Option<u16>,
58    pub headers: Option<HashMap<String, String>>,
59}
60
61#[derive(Debug, Error)]
62pub enum LlmixError {
63    #[error("{0}")]
64    InvalidResponseCacheConfig(String),
65
66    #[error("{0}")]
67    InvalidKeyPoolConfig(String),
68
69    #[error("{0}")]
70    InvalidAdaptiveSemaphoreConfig(String),
71
72    #[error("{0}")]
73    InvalidRetryPolicyConfig(String),
74
75    #[error("{0}")]
76    InvalidFileLockConfig(String),
77
78    #[error("{0}")]
79    InvalidProviderKwargsConfig(String),
80
81    #[error("redis error: {0}")]
82    Redis(String),
83
84    #[error("{0}")]
85    UnknownKeyPoolKey(String),
86
87    #[error("failed to serialize canonical json: {0}")]
88    CanonicalJson(#[from] serde_json::Error),
89
90    #[error(transparent)]
91    Io(#[from] std::io::Error),
92
93    #[error(transparent)]
94    CircuitOpen(#[from] CircuitOpenError),
95
96    #[error(transparent)]
97    KillSwitchActive(#[from] KillSwitchActiveError),
98
99    #[error(transparent)]
100    KeyPoolExhausted(#[from] KeyPoolExhaustedError),
101
102    #[error(transparent)]
103    ConfigNotFound(#[from] ConfigNotFoundError),
104
105    #[error(transparent)]
106    ConfigAccess(#[from] ConfigAccessError),
107
108    #[error(transparent)]
109    InvalidConfig(#[from] InvalidConfigError),
110
111    #[error(transparent)]
112    Security(#[from] SecurityError),
113
114    #[error(transparent)]
115    AdaptiveSemaphoreClosed(#[from] AdaptiveSemaphoreClosedError),
116
117    #[error(transparent)]
118    Provider(#[from] ProviderError),
119}