|
| 1 | +//! Error types for the Destroy command handler |
| 2 | +
|
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use crate::adapters::tofu::client::OpenTofuError; |
| 6 | +use crate::domain::environment::state::StateTypeError; |
| 7 | +use crate::shared::command::CommandError; |
| 8 | + |
| 9 | +/// Comprehensive error type for the `DestroyCommandHandler` |
| 10 | +#[derive(Debug, thiserror::Error)] |
| 11 | +pub enum DestroyCommandHandlerError { |
| 12 | + #[error("OpenTofu command failed: {0}")] |
| 13 | + OpenTofu(#[from] OpenTofuError), |
| 14 | + |
| 15 | + #[error("Command execution failed: {0}")] |
| 16 | + Command(#[from] CommandError), |
| 17 | + |
| 18 | + #[error("Failed to persist environment state: {0}")] |
| 19 | + StatePersistence(#[from] crate::domain::environment::repository::RepositoryError), |
| 20 | + |
| 21 | + #[error("Invalid state transition: {0}")] |
| 22 | + StateTransition(#[from] StateTypeError), |
| 23 | + |
| 24 | + #[error("Failed to clean up state files at '{path}': {source}")] |
| 25 | + StateCleanupFailed { |
| 26 | + path: PathBuf, |
| 27 | + #[source] |
| 28 | + source: std::io::Error, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +impl crate::shared::Traceable for DestroyCommandHandlerError { |
| 33 | + fn trace_format(&self) -> String { |
| 34 | + match self { |
| 35 | + Self::OpenTofu(e) => { |
| 36 | + format!("DestroyCommandHandlerError: OpenTofu command failed - {e}") |
| 37 | + } |
| 38 | + Self::Command(e) => { |
| 39 | + format!("DestroyCommandHandlerError: Command execution failed - {e}") |
| 40 | + } |
| 41 | + Self::StatePersistence(e) => { |
| 42 | + format!("DestroyCommandHandlerError: Failed to persist environment state - {e}") |
| 43 | + } |
| 44 | + Self::StateTransition(e) => { |
| 45 | + format!("DestroyCommandHandlerError: Invalid state transition - {e}") |
| 46 | + } |
| 47 | + Self::StateCleanupFailed { path, source } => { |
| 48 | + format!( |
| 49 | + "DestroyCommandHandlerError: Failed to clean up state files at '{}' - {source}", |
| 50 | + path.display() |
| 51 | + ) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fn trace_source(&self) -> Option<&dyn crate::shared::Traceable> { |
| 57 | + match self { |
| 58 | + Self::OpenTofu(e) => Some(e), |
| 59 | + Self::Command(e) => Some(e), |
| 60 | + Self::StatePersistence(_) |
| 61 | + | Self::StateTransition(_) |
| 62 | + | Self::StateCleanupFailed { .. } => None, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fn error_kind(&self) -> crate::shared::ErrorKind { |
| 67 | + match self { |
| 68 | + Self::OpenTofu(_) => crate::shared::ErrorKind::InfrastructureOperation, |
| 69 | + Self::Command(_) => crate::shared::ErrorKind::CommandExecution, |
| 70 | + Self::StateTransition(_) => crate::shared::ErrorKind::Configuration, |
| 71 | + Self::StatePersistence(_) | Self::StateCleanupFailed { .. } => { |
| 72 | + crate::shared::ErrorKind::StatePersistence |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments