1use std::io;
4use std::path::PathBuf;
5use thiserror::Error;
6
7pub type ScalingResult<T> = std::result::Result<T, ScalingError>;
9
10#[derive(Error, Debug)]
12pub enum ScalingError {
13 #[error("I/O error: {message}")]
15 Io {
16 message: String,
17 #[source]
18 source: io::Error,
19 },
20
21 #[error("Path error: {message} (path: {path:?})")]
23 Path {
24 message: String,
25 path: PathBuf,
26 #[source]
27 source: Option<io::Error>,
28 },
29
30 #[error("Configuration error: {message}")]
32 Config {
33 message: String,
34 field: Option<String>,
35 },
36
37 #[error("Cache error: {message}")]
39 Cache {
40 message: String,
41 #[source]
42 source: Option<Box<dyn std::error::Error + Send + Sync>>,
43 },
44
45 #[error("Streaming error: {message}")]
47 Streaming {
48 message: String,
49 #[source]
50 source: Option<Box<dyn std::error::Error + Send + Sync>>,
51 },
52
53 #[error("Parallel processing error: {message}")]
55 Parallel {
56 message: String,
57 #[source]
58 source: Option<Box<dyn std::error::Error + Send + Sync>>,
59 },
60
61 #[error("Memory error: {message}")]
63 Memory {
64 message: String,
65 details: Option<String>,
66 },
67
68 #[error("Signature error: {message}")]
70 Signature {
71 message: String,
72 #[source]
73 source: Option<Box<dyn std::error::Error + Send + Sync>>,
74 },
75
76 #[error("Profiling error: {message}")]
78 Profiling {
79 message: String,
80 #[source]
81 source: Option<Box<dyn std::error::Error + Send + Sync>>,
82 },
83
84 #[error("Timeout error: {message} (timeout: {timeout_ms}ms)")]
86 Timeout { message: String, timeout_ms: u64 },
87
88 #[error("Resource limit exceeded: {message} (limit: {limit}, actual: {actual})")]
90 ResourceLimit {
91 message: String,
92 limit: u64,
93 actual: u64,
94 },
95
96 #[error("Internal error: {message}")]
98 Internal {
99 message: String,
100 location: Option<String>,
101 },
102}
103
104impl ScalingError {
105 pub fn io<S: Into<String>>(message: S, source: io::Error) -> Self {
107 Self::Io {
108 message: message.into(),
109 source,
110 }
111 }
112
113 pub fn path<S: Into<String>, P: Into<PathBuf>>(message: S, path: P) -> Self {
115 Self::Path {
116 message: message.into(),
117 path: path.into(),
118 source: None,
119 }
120 }
121
122 pub fn config<S: Into<String>>(message: S) -> Self {
124 Self::Config {
125 message: message.into(),
126 field: None,
127 }
128 }
129
130 pub fn cache<S: Into<String>>(message: S) -> Self {
132 Self::Cache {
133 message: message.into(),
134 source: None,
135 }
136 }
137
138 pub fn streaming<S: Into<String>>(message: S) -> Self {
140 Self::Streaming {
141 message: message.into(),
142 source: None,
143 }
144 }
145
146 pub fn parallel<S: Into<String>>(message: S) -> Self {
148 Self::Parallel {
149 message: message.into(),
150 source: None,
151 }
152 }
153
154 pub fn memory<S: Into<String>>(message: S) -> Self {
156 Self::Memory {
157 message: message.into(),
158 details: None,
159 }
160 }
161
162 pub fn signature<S: Into<String>>(message: S) -> Self {
164 Self::Signature {
165 message: message.into(),
166 source: None,
167 }
168 }
169
170 pub fn profiling<S: Into<String>>(message: S) -> Self {
172 Self::Profiling {
173 message: message.into(),
174 source: None,
175 }
176 }
177
178 pub fn timeout<S: Into<String>>(message: S, timeout_ms: u64) -> Self {
180 Self::Timeout {
181 message: message.into(),
182 timeout_ms,
183 }
184 }
185
186 pub fn resource_limit<S: Into<String>>(message: S, limit: u64, actual: u64) -> Self {
188 Self::ResourceLimit {
189 message: message.into(),
190 limit,
191 actual,
192 }
193 }
194
195 pub fn internal<S: Into<String>>(message: S) -> Self {
197 Self::Internal {
198 message: message.into(),
199 location: None,
200 }
201 }
202}
203
204impl From<io::Error> for ScalingError {
205 fn from(error: io::Error) -> Self {
206 Self::Io {
207 message: "I/O operation failed".to_string(),
208 source: error,
209 }
210 }
211}
212
213impl From<serde_json::Error> for ScalingError {
214 fn from(error: serde_json::Error) -> Self {
215 Self::Config {
216 message: format!("JSON serialization/deserialization failed: {}", error),
217 field: None,
218 }
219 }
220}
221
222impl From<bincode::Error> for ScalingError {
223 fn from(error: bincode::Error) -> Self {
224 Self::Cache {
225 message: format!("Binary serialization failed: {}", error),
226 source: Some(Box::new(error)),
227 }
228 }
229}