scirs2_transform/
error.rs1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum TransformError {
8 #[error("Invalid input: {0}")]
10 InvalidInput(String),
11
12 #[error("Transformation error: {0}")]
14 TransformationError(String),
15
16 #[error("Core error: {0}")]
18 CoreError(#[from] scirs2_core::error::CoreError),
19
20 #[error("Linear algebra error: {0}")]
22 LinalgError(#[from] scirs2_linalg::error::LinalgError),
23
24 #[error("FFT error: {0}")]
26 FFTError(#[from] scirs2_fft::error::FFTError),
27
28 #[error("IO error: {0}")]
30 IoError(#[from] std::io::Error),
31
32 #[error("Computation error: {0}")]
34 ComputationError(String),
35
36 #[error("Model not fitted: {0}")]
38 NotFitted(String),
39
40 #[error("Feature not enabled: {0}")]
42 FeatureNotEnabled(String),
43
44 #[error("GPU error: {0}")]
46 GpuError(String),
47
48 #[error("Distributed processing error: {0}")]
50 DistributedError(String),
51
52 #[error("Monitoring error: {0}")]
54 MonitoringError(String),
55
56 #[error("Memory error: {0}")]
58 MemoryError(String),
59
60 #[error("Convergence error: {0}")]
62 ConvergenceError(String),
63
64 #[error("Data validation error: {0}")]
66 DataValidationError(String),
67
68 #[error("Parallel processing error: {0}")]
70 ParallelError(String),
71
72 #[error("Configuration error: {0}")]
74 ConfigurationError(String),
75
76 #[error("Timeout error: {0}")]
78 TimeoutError(String),
79
80 #[error("SIMD error: {0}")]
82 SimdError(String),
83
84 #[error("Streaming error: {0}")]
86 StreamingError(String),
87
88 #[error("Cross-validation error: {0}")]
90 CrossValidationError(String),
91
92 #[cfg(feature = "monitoring")]
94 #[error("Prometheus error: {0}")]
95 PrometheusError(#[from] prometheus::Error),
96
97 #[cfg(feature = "distributed")]
99 #[error("Serialization error: {0}")]
100 SerializationError(#[from] bincode::error::EncodeError),
101
102 #[error("Not implemented: {0}")]
104 NotImplemented(String),
105
106 #[error("Parse error: {0}")]
108 ParseError(String),
109
110 #[cfg(feature = "auto-feature-engineering")]
112 #[error("PyTorch error: {0}")]
113 TchError(#[from] tch::TchError),
114
115 #[error("Error: {0}")]
117 Other(String),
118}
119
120pub type Result<T> = std::result::Result<T, TransformError>;
122
123pub trait ErrorContext<T> {
125 fn context(self, msg: &str) -> Result<T>;
127
128 fn with_context<F>(self, f: F) -> Result<T>
130 where
131 F: FnOnce() -> String;
132}
133
134impl<T> ErrorContext<T> for Result<T> {
135 fn context(self, msg: &str) -> Result<T> {
136 self.map_err(|e| TransformError::Other(format!("{msg}: {e}")))
137 }
138
139 fn with_context<F>(self, f: F) -> Result<T>
140 where
141 F: FnOnce() -> String,
142 {
143 self.map_err(|e| TransformError::Other(format!("{}: {e}", f())))
144 }
145}
146
147#[derive(Debug, Clone, PartialEq, Eq)]
149pub enum ErrorKind {
150 Validation,
152 Computation,
154 Configuration,
156 Resource,
158 External,
160 Internal,
162}
163
164impl TransformError {
165 pub fn kind(&self) -> ErrorKind {
167 match self {
168 TransformError::InvalidInput(_)
169 | TransformError::DataValidationError(_)
170 | TransformError::ConfigurationError(_) => ErrorKind::Validation,
171
172 TransformError::ComputationError(_)
173 | TransformError::TransformationError(_)
174 | TransformError::ConvergenceError(_)
175 | TransformError::SimdError(_) => ErrorKind::Computation,
176
177 TransformError::MemoryError(_)
178 | TransformError::GpuError(_)
179 | TransformError::TimeoutError(_) => ErrorKind::Resource,
180
181 TransformError::IoError(_)
182 | TransformError::DistributedError(_)
183 | TransformError::StreamingError(_)
184 | TransformError::MonitoringError(_) => ErrorKind::External,
185
186 TransformError::NotImplemented(_)
187 | TransformError::NotFitted(_)
188 | TransformError::FeatureNotEnabled(_)
189 | TransformError::Other(_) => ErrorKind::Internal,
190
191 TransformError::CoreError(_)
192 | TransformError::LinalgError(_)
193 | TransformError::FFTError(_) => ErrorKind::External,
194
195 TransformError::ParallelError(_)
196 | TransformError::CrossValidationError(_)
197 | TransformError::ParseError(_) => ErrorKind::Computation,
198
199 #[cfg(feature = "monitoring")]
200 TransformError::PrometheusError(_) => ErrorKind::External,
201
202 #[cfg(feature = "distributed")]
203 TransformError::SerializationError(_) => ErrorKind::External,
204
205 #[cfg(feature = "auto-feature-engineering")]
206 TransformError::TchError(_) => ErrorKind::Computation,
207 }
208 }
209
210 pub fn is_recoverable(&self) -> bool {
212 match self.kind() {
213 ErrorKind::Validation | ErrorKind::Configuration => false,
214 ErrorKind::Resource | ErrorKind::External => true,
215 ErrorKind::Computation => true, ErrorKind::Internal => false,
217 }
218 }
219
220 pub fn should_retry(&self) -> bool {
222 matches!(
223 self,
224 TransformError::TimeoutError(_)
225 | TransformError::MemoryError(_)
226 | TransformError::DistributedError(_)
227 | TransformError::StreamingError(_)
228 | TransformError::ParallelError(_)
229 | TransformError::IoError(_)
230 )
231 }
232
233 pub fn user_message(&self) -> String {
235 match self {
236 TransformError::InvalidInput(_) => "Invalid input data provided".to_string(),
237 TransformError::NotFitted(_) => "Model must be fitted before use".to_string(),
238 TransformError::MemoryError(_) => "Insufficient memory for operation".to_string(),
239 TransformError::TimeoutError(_) => "Operation timed out".to_string(),
240 TransformError::FeatureNotEnabled(_) => "Required feature is not enabled".to_string(),
241 _ => "An error occurred during transformation".to_string(),
242 }
243 }
244}