scribe_scaling/
error.rs

1//! Error handling for scaling optimizations.
2
3use std::io;
4use std::path::PathBuf;
5use thiserror::Error;
6
7/// Type alias for Results using ScalingError
8pub type ScalingResult<T> = std::result::Result<T, ScalingError>;
9
10/// Scaling-specific error types
11#[derive(Error, Debug)]
12pub enum ScalingError {
13    /// I/O related errors
14    #[error("I/O error: {message}")]
15    Io {
16        message: String,
17        #[source]
18        source: io::Error,
19    },
20
21    /// Path-related errors
22    #[error("Path error: {message} (path: {path:?})")]
23    Path {
24        message: String,
25        path: PathBuf,
26        #[source]
27        source: Option<io::Error>,
28    },
29
30    /// Configuration errors
31    #[error("Configuration error: {message}")]
32    Config {
33        message: String,
34        field: Option<String>,
35    },
36
37    /// Caching errors
38    #[error("Cache error: {message}")]
39    Cache {
40        message: String,
41        #[source]
42        source: Option<Box<dyn std::error::Error + Send + Sync>>,
43    },
44
45    /// Streaming errors
46    #[error("Streaming error: {message}")]
47    Streaming {
48        message: String,
49        #[source]
50        source: Option<Box<dyn std::error::Error + Send + Sync>>,
51    },
52
53    /// Parallel processing errors
54    #[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    /// Memory management errors
62    #[error("Memory error: {message}")]
63    Memory {
64        message: String,
65        details: Option<String>,
66    },
67
68    /// Signature extraction errors
69    #[error("Signature error: {message}")]
70    Signature {
71        message: String,
72        #[source]
73        source: Option<Box<dyn std::error::Error + Send + Sync>>,
74    },
75
76    /// Repository profiling errors
77    #[error("Profiling error: {message}")]
78    Profiling {
79        message: String,
80        #[source]
81        source: Option<Box<dyn std::error::Error + Send + Sync>>,
82    },
83
84    /// Timeout errors
85    #[error("Timeout error: {message} (timeout: {timeout_ms}ms)")]
86    Timeout { message: String, timeout_ms: u64 },
87
88    /// Resource limit exceeded
89    #[error("Resource limit exceeded: {message} (limit: {limit}, actual: {actual})")]
90    ResourceLimit {
91        message: String,
92        limit: u64,
93        actual: u64,
94    },
95
96    /// Internal errors
97    #[error("Internal error: {message}")]
98    Internal {
99        message: String,
100        location: Option<String>,
101    },
102}
103
104impl ScalingError {
105    /// Create a new I/O error
106    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    /// Create a new path error
114    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    /// Create a new configuration error
123    pub fn config<S: Into<String>>(message: S) -> Self {
124        Self::Config {
125            message: message.into(),
126            field: None,
127        }
128    }
129
130    /// Create a new cache error
131    pub fn cache<S: Into<String>>(message: S) -> Self {
132        Self::Cache {
133            message: message.into(),
134            source: None,
135        }
136    }
137
138    /// Create a new streaming error
139    pub fn streaming<S: Into<String>>(message: S) -> Self {
140        Self::Streaming {
141            message: message.into(),
142            source: None,
143        }
144    }
145
146    /// Create a new parallel processing error
147    pub fn parallel<S: Into<String>>(message: S) -> Self {
148        Self::Parallel {
149            message: message.into(),
150            source: None,
151        }
152    }
153
154    /// Create a new memory error
155    pub fn memory<S: Into<String>>(message: S) -> Self {
156        Self::Memory {
157            message: message.into(),
158            details: None,
159        }
160    }
161
162    /// Create a new signature error
163    pub fn signature<S: Into<String>>(message: S) -> Self {
164        Self::Signature {
165            message: message.into(),
166            source: None,
167        }
168    }
169
170    /// Create a new profiling error
171    pub fn profiling<S: Into<String>>(message: S) -> Self {
172        Self::Profiling {
173            message: message.into(),
174            source: None,
175        }
176    }
177
178    /// Create a new timeout error
179    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    /// Create a new resource limit error
187    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    /// Create a new internal error
196    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}