1use std::time::Duration;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Error, Debug)]
11pub enum Error {
12 #[error("Model not found: {model_id}")]
14 ModelNotFound {
15 model_id: String,
17 },
18
19 #[error("Unsupported model architecture: {architecture}")]
21 UnsupportedArchitecture {
22 architecture: String,
24 },
25
26 #[error("Out of memory: requested {requested} bytes, available {available} bytes")]
28 OutOfMemory {
29 requested: usize,
31 available: usize,
33 },
34
35 #[error("Context length exceeded: {current} tokens > {max} max tokens")]
37 ContextLengthExceeded {
38 current: u32,
40 max: u32,
42 },
43
44 #[error("Invalid configuration: {message}")]
46 InvalidConfig {
47 message: String,
49 },
50
51 #[error("Backend error: {message}")]
53 Backend {
54 backend: String,
56 message: String,
58 },
59
60 #[error("Operation timed out after {duration:?}")]
62 Timeout {
63 duration: Duration,
65 },
66
67 #[error("Rate limited: retry after {retry_after:?}")]
69 RateLimited {
70 retry_after: Duration,
72 },
73
74 #[error("Tokenization error: {message}")]
76 Tokenization {
77 message: String,
79 },
80
81 #[error("Failed to load model: {message}")]
83 ModelLoad {
84 message: String,
86 },
87
88 #[error("I/O error: {0}")]
90 Io(#[from] std::io::Error),
91
92 #[error("Serialization error: {0}")]
94 Serialization(#[from] serde_json::Error),
95
96 #[error("Internal error: {message}")]
98 Internal {
99 message: String,
101 },
102}
103
104impl Error {
105 #[must_use]
107 pub fn is_retryable(&self) -> bool {
108 matches!(self, Self::Timeout { .. } | Self::RateLimited { .. })
109 }
110
111 #[must_use]
113 pub fn is_resource_exhaustion(&self) -> bool {
114 matches!(
115 self,
116 Self::OutOfMemory { .. } | Self::ContextLengthExceeded { .. }
117 )
118 }
119
120 #[must_use]
122 pub fn internal(message: impl Into<String>) -> Self {
123 Self::Internal {
124 message: message.into(),
125 }
126 }
127
128 #[must_use]
130 pub fn backend(backend: impl Into<String>, message: impl Into<String>) -> Self {
131 Self::Backend {
132 backend: backend.into(),
133 message: message.into(),
134 }
135 }
136
137 #[must_use]
139 pub fn model_load(message: impl Into<String>) -> Self {
140 Self::ModelLoad {
141 message: message.into(),
142 }
143 }
144}