1use std::path::PathBuf;
7use thiserror::Error;
8
9#[derive(Error, Debug)]
11pub enum LifecycleError {
12 #[error("Failed to load configuration from {path}: {source}")]
14 ConfigLoad {
15 path: PathBuf,
16 #[source]
17 source: Box<dyn std::error::Error + Send + Sync>,
18 },
19
20 #[error("Failed to parse TOML configuration at {path}: {source}")]
22 ConfigParse {
23 path: PathBuf,
24 #[source]
25 source: toml::de::Error,
26 },
27
28 #[error("Phase '{phase}' not found in configuration")]
30 PhaseNotFound { phase: String },
31
32 #[error("Phase '{phase}' has no commands defined")]
34 NoCommands { phase: String },
35
36 #[error("Command failed in phase '{phase}': {command}\n Exit code: {exit_code}\n Stderr: {stderr}")]
38 CommandFailed {
39 phase: String,
40 command: String,
41 exit_code: i32,
42 stderr: String,
43 },
44
45 #[error("Failed to spawn command in phase '{phase}': {command}")]
47 CommandSpawn {
48 phase: String,
49 command: String,
50 #[source]
51 source: std::io::Error,
52 },
53
54 #[error("Hook recursion detected: phase '{phase}' called recursively through chain: {}", chain.join(" -> "))]
56 HookRecursion { phase: String, chain: Vec<String> },
57
58 #[error("Hook failed for phase '{phase}': {hook_phase}")]
60 HookFailed {
61 phase: String,
62 hook_phase: String,
63 #[source]
64 source: Box<LifecycleError>,
65 },
66
67 #[error("Failed to load state from {path}: {source}")]
69 StateLoad {
70 path: PathBuf,
71 #[source]
72 source: std::io::Error,
73 },
74
75 #[error("Failed to parse state JSON from {path}: {source}")]
77 StateParse {
78 path: PathBuf,
79 #[source]
80 source: serde_json::Error,
81 },
82
83 #[error("Failed to save state to {path}: {source}")]
85 StateSave {
86 path: PathBuf,
87 #[source]
88 source: std::io::Error,
89 },
90
91 #[error("Invalid cache path: {path}")]
93 InvalidCachePath { path: PathBuf },
94
95 #[error("Failed to create cache directory for phase '{phase}': {source}")]
97 CacheCreate {
98 phase: String,
99 #[source]
100 source: std::io::Error,
101 },
102
103 #[error("File I/O error at {path}: {source}")]
105 FileIo {
106 path: PathBuf,
107 #[source]
108 source: std::io::Error,
109 },
110
111 #[error("Failed to create directory {path}: {source}")]
113 DirectoryCreate {
114 path: PathBuf,
115 #[source]
116 source: std::io::Error,
117 },
118
119 #[error("Failed to load make.toml from {path}: {source}")]
121 MakeTomlLoad {
122 path: PathBuf,
123 #[source]
124 source: std::io::Error,
125 },
126
127 #[error("Failed to parse make.toml at {path}: {source}")]
129 MakeTomlParse {
130 path: PathBuf,
131 #[source]
132 source: toml::de::Error,
133 },
134
135 #[error("Workspace '{workspace}' not found in configuration")]
137 WorkspaceNotFound { workspace: String },
138
139 #[error("Invalid workspace path '{workspace}': {path}")]
141 WorkspacePath { workspace: String, path: PathBuf },
142
143 #[error("Parallel execution failed for workspace '{workspace}': {source}")]
145 ParallelExecution {
146 workspace: String,
147 #[source]
148 source: Box<LifecycleError>,
149 },
150
151 #[error("Invalid environment variable: {key}={value}")]
153 InvalidEnv { key: String, value: String },
154
155 #[error("Internal error: mutex poisoned in phase '{phase}'")]
157 MutexPoisoned { phase: String },
158
159 #[error("I/O error: {0}")]
161 Io(#[from] std::io::Error),
162
163 #[error("Circular dependency detected in phase dependencies: {phases}")]
165 DependencyCycle { phases: String },
166
167 #[error("{0}")]
169 Other(String),
170}
171
172impl LifecycleError {
173 pub fn config_load(
175 path: impl Into<PathBuf>, source: impl std::error::Error + Send + Sync + 'static,
176 ) -> Self {
177 Self::ConfigLoad {
178 path: path.into(),
179 source: Box::new(source),
180 }
181 }
182
183 pub fn config_parse(path: impl Into<PathBuf>, source: toml::de::Error) -> Self {
185 Self::ConfigParse {
186 path: path.into(),
187 source,
188 }
189 }
190
191 pub fn phase_not_found(phase: impl Into<String>) -> Self {
193 Self::PhaseNotFound {
194 phase: phase.into(),
195 }
196 }
197
198 pub fn command_failed(
200 phase: impl Into<String>, command: impl Into<String>, exit_code: i32,
201 stderr: impl Into<String>,
202 ) -> Self {
203 Self::CommandFailed {
204 phase: phase.into(),
205 command: command.into(),
206 exit_code,
207 stderr: stderr.into(),
208 }
209 }
210
211 pub fn command_spawn(
213 phase: impl Into<String>, command: impl Into<String>, source: std::io::Error,
214 ) -> Self {
215 Self::CommandSpawn {
216 phase: phase.into(),
217 command: command.into(),
218 source,
219 }
220 }
221
222 pub fn hook_recursion_with_chain(phase: impl Into<String>, chain: Vec<String>) -> Self {
224 Self::HookRecursion {
225 phase: phase.into(),
226 chain,
227 }
228 }
229
230 pub fn hook_recursion(phase: impl Into<String>) -> Self {
232 let phase_str = phase.into();
233 Self::HookRecursion {
234 phase: phase_str.clone(),
235 chain: vec![phase_str],
236 }
237 }
238
239 pub fn state_load(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
241 Self::StateLoad {
242 path: path.into(),
243 source,
244 }
245 }
246
247 pub fn state_parse(path: impl Into<PathBuf>, source: serde_json::Error) -> Self {
249 Self::StateParse {
250 path: path.into(),
251 source,
252 }
253 }
254
255 pub fn state_save(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
257 Self::StateSave {
258 path: path.into(),
259 source,
260 }
261 }
262
263 pub fn invalid_cache_path(path: impl Into<PathBuf>) -> Self {
265 Self::InvalidCachePath { path: path.into() }
266 }
267
268 pub fn cache_create(phase: impl Into<String>, source: std::io::Error) -> Self {
270 Self::CacheCreate {
271 phase: phase.into(),
272 source,
273 }
274 }
275
276 pub fn file_io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
278 Self::FileIo {
279 path: path.into(),
280 source,
281 }
282 }
283
284 pub fn directory_create(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
286 Self::DirectoryCreate {
287 path: path.into(),
288 source,
289 }
290 }
291
292 pub fn make_toml_load(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
294 Self::MakeTomlLoad {
295 path: path.into(),
296 source,
297 }
298 }
299
300 pub fn make_toml_parse(path: impl Into<PathBuf>, source: toml::de::Error) -> Self {
302 Self::MakeTomlParse {
303 path: path.into(),
304 source,
305 }
306 }
307
308 pub fn parallel_execution(workspace: impl Into<String>, source: LifecycleError) -> Self {
310 Self::ParallelExecution {
311 workspace: workspace.into(),
312 source: Box::new(source),
313 }
314 }
315
316 pub fn dependency_cycle(phases: impl Into<String>) -> Self {
318 Self::DependencyCycle {
319 phases: phases.into(),
320 }
321 }
322}
323
324pub type Result<T> = std::result::Result<T, LifecycleError>;
326
327#[cfg(test)]
328mod tests {
329 use super::*;
330
331 #[test]
332 fn test_phase_not_found_error() {
333 let err = LifecycleError::phase_not_found("build");
334 assert_eq!(err.to_string(), "Phase 'build' not found in configuration");
335 }
336
337 #[test]
338 fn test_command_failed_error() {
339 let err = LifecycleError::command_failed("test", "cargo test", 101, "test failed");
340 let msg = err.to_string();
341 assert!(msg.contains("Command failed"));
342 assert!(msg.contains("Exit code: 101"));
343 assert!(msg.contains("test failed"));
344 }
345
346 #[test]
347 fn test_hook_recursion_error() {
348 let err = LifecycleError::hook_recursion("build");
349 let msg = err.to_string();
350 assert!(msg.contains("Hook recursion detected"));
351 assert!(msg.contains("build"));
352 }
353
354 #[test]
355 fn test_hook_recursion_with_chain() {
356 let chain = vec![
357 "init".to_string(),
358 "setup".to_string(),
359 "build".to_string(),
360 "init".to_string(),
361 ];
362 let err = LifecycleError::hook_recursion_with_chain("init", chain);
363 let msg = err.to_string();
364 assert!(msg.contains("init -> setup -> build -> init"));
365 }
366
367 #[test]
368 fn test_state_errors() {
369 let path = PathBuf::from("/tmp/state.json");
370
371 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
373 let err = LifecycleError::state_load(&path, io_err);
374 assert!(err.to_string().contains("Failed to load state"));
375
376 let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied");
378 let err = LifecycleError::state_save(&path, io_err);
379 assert!(err.to_string().contains("Failed to save state"));
380 }
381
382 #[test]
383 fn test_make_toml_errors() {
384 let path = PathBuf::from("/tmp/make.toml");
385
386 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
388 let err = LifecycleError::make_toml_load(&path, io_err);
389 assert!(err.to_string().contains("Failed to load make.toml"));
390 assert!(err.to_string().contains("/tmp/make.toml"));
391 }
392
393 #[test]
394 fn test_cache_errors() {
395 let path = PathBuf::from("/tmp/cache");
396
397 let err = LifecycleError::invalid_cache_path(&path);
399 assert!(err.to_string().contains("Invalid cache path"));
400
401 let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied");
403 let err = LifecycleError::cache_create("build", io_err);
404 assert!(err.to_string().contains("Failed to create cache directory"));
405 assert!(err.to_string().contains("build"));
406 }
407
408 #[test]
409 fn test_error_downcasting() {
410 let err = LifecycleError::phase_not_found("build");
411 let err_ref: &dyn std::error::Error = &err;
412 assert!(err_ref.source().is_none());
413
414 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
415 let err = LifecycleError::state_load("/tmp/state.json", io_err);
416 let err_ref: &dyn std::error::Error = &err;
417 assert!(err_ref.source().is_some());
418 }
419
420 #[test]
421 fn test_error_display() {
422 let lifecycle_err = LifecycleError::phase_not_found("test");
423 let err_str = lifecycle_err.to_string();
424 assert!(err_str.contains("Phase 'test' not found"));
425 }
426}