Skip to main content

terraform_wrapper/
error.rs

1use thiserror::Error;
2
3/// Result type for terraform-wrapper operations.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Error type for terraform-wrapper operations.
7#[derive(Error, Debug)]
8pub enum Error {
9    /// Terraform binary not found.
10    #[error("terraform binary not found")]
11    NotFound,
12
13    /// Command exited with non-zero status.
14    #[error("terraform command failed: {command} (exit code {exit_code})")]
15    CommandFailed {
16        /// The subcommand that failed (e.g. "init", "plan").
17        command: String,
18        /// Process exit code.
19        exit_code: i32,
20        /// Captured stdout.
21        stdout: String,
22        /// Captured stderr.
23        stderr: String,
24    },
25
26    /// IO error during subprocess execution.
27    #[error("io error: {message}")]
28    Io {
29        /// Description of the IO operation.
30        message: String,
31        /// Underlying IO error.
32        #[source]
33        source: std::io::Error,
34    },
35
36    /// Command execution timed out.
37    #[error("terraform command timed out after {timeout_seconds}s")]
38    Timeout {
39        /// The timeout duration in seconds.
40        timeout_seconds: u64,
41    },
42
43    /// JSON deserialization error.
44    #[cfg(feature = "json")]
45    #[error("json parse error: {message}")]
46    Json {
47        /// Description of what was being parsed.
48        message: String,
49        /// Underlying serde_json error.
50        #[source]
51        source: serde_json::Error,
52    },
53}
54
55impl From<std::io::Error> for Error {
56    fn from(e: std::io::Error) -> Self {
57        if e.kind() == std::io::ErrorKind::NotFound {
58            Error::NotFound
59        } else {
60            Error::Io {
61                message: e.to_string(),
62                source: e,
63            }
64        }
65    }
66}