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    /// Failed to parse command output.
27    #[error("failed to parse terraform output: {message}")]
28    ParseError {
29        /// Description of what failed to parse.
30        message: String,
31    },
32
33    /// IO error during subprocess execution.
34    #[error("io error: {message}")]
35    Io {
36        /// Description of the IO operation.
37        message: String,
38        /// Underlying IO error.
39        #[source]
40        source: std::io::Error,
41    },
42
43    /// Command execution timed out.
44    #[error("terraform command timed out after {timeout_seconds}s")]
45    Timeout {
46        /// The timeout duration in seconds.
47        timeout_seconds: u64,
48    },
49
50    /// JSON deserialization error.
51    #[cfg(feature = "json")]
52    #[error("json parse error: {message}")]
53    Json {
54        /// Description of what was being parsed.
55        message: String,
56        /// Underlying serde_json error.
57        #[source]
58        source: serde_json::Error,
59    },
60}
61
62impl From<std::io::Error> for Error {
63    fn from(e: std::io::Error) -> Self {
64        if e.kind() == std::io::ErrorKind::NotFound {
65            Error::NotFound
66        } else {
67            Error::Io {
68                message: e.to_string(),
69                source: e,
70            }
71        }
72    }
73}