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 {
87 message: String,
88 timeout_ms: u64,
89 },
90
91 #[error("Resource limit exceeded: {message} (limit: {limit}, actual: {actual})")]
93 ResourceLimit {
94 message: String,
95 limit: u64,
96 actual: u64,
97 },
98
99 #[error("Internal error: {message}")]
101 Internal {
102 message: String,
103 location: Option<String>,
104 },
105}
106
107impl ScalingError {
108 pub fn io<S: Into<String>>(message: S, source: io::Error) -> Self {
110 Self::Io {
111 message: message.into(),
112 source,
113 }
114 }
115
116 pub fn path<S: Into<String>, P: Into<PathBuf>>(message: S, path: P) -> Self {
118 Self::Path {
119 message: message.into(),
120 path: path.into(),
121 source: None,
122 }
123 }
124
125 pub fn config<S: Into<String>>(message: S) -> Self {
127 Self::Config {
128 message: message.into(),
129 field: None,
130 }
131 }
132
133 pub fn cache<S: Into<String>>(message: S) -> Self {
135 Self::Cache {
136 message: message.into(),
137 source: None,
138 }
139 }
140
141 pub fn streaming<S: Into<String>>(message: S) -> Self {
143 Self::Streaming {
144 message: message.into(),
145 source: None,
146 }
147 }
148
149 pub fn parallel<S: Into<String>>(message: S) -> Self {
151 Self::Parallel {
152 message: message.into(),
153 source: None,
154 }
155 }
156
157 pub fn memory<S: Into<String>>(message: S) -> Self {
159 Self::Memory {
160 message: message.into(),
161 details: None,
162 }
163 }
164
165 pub fn signature<S: Into<String>>(message: S) -> Self {
167 Self::Signature {
168 message: message.into(),
169 source: None,
170 }
171 }
172
173 pub fn profiling<S: Into<String>>(message: S) -> Self {
175 Self::Profiling {
176 message: message.into(),
177 source: None,
178 }
179 }
180
181 pub fn timeout<S: Into<String>>(message: S, timeout_ms: u64) -> Self {
183 Self::Timeout {
184 message: message.into(),
185 timeout_ms,
186 }
187 }
188
189 pub fn resource_limit<S: Into<String>>(message: S, limit: u64, actual: u64) -> Self {
191 Self::ResourceLimit {
192 message: message.into(),
193 limit,
194 actual,
195 }
196 }
197
198 pub fn internal<S: Into<String>>(message: S) -> Self {
200 Self::Internal {
201 message: message.into(),
202 location: None,
203 }
204 }
205}
206
207impl From<io::Error> for ScalingError {
208 fn from(error: io::Error) -> Self {
209 Self::Io {
210 message: "I/O operation failed".to_string(),
211 source: error,
212 }
213 }
214}
215
216impl From<serde_json::Error> for ScalingError {
217 fn from(error: serde_json::Error) -> Self {
218 Self::Config {
219 message: format!("JSON serialization/deserialization failed: {}", error),
220 field: None,
221 }
222 }
223}
224
225impl From<bincode::Error> for ScalingError {
226 fn from(error: bincode::Error) -> Self {
227 Self::Cache {
228 message: format!("Binary serialization failed: {}", error),
229 source: Some(Box::new(error)),
230 }
231 }
232}