Skip to main content

bare_script/
error.rs

1//! Error types for script command operations.
2
3use thiserror::Error;
4
5/// Error type for script command operations.
6#[derive(Error, Debug)]
7pub enum ScriptError {
8    /// Command not found.
9    #[error("Command not found: {0}")]
10    CommandNotFound(String),
11
12    /// Permission denied.
13    #[error("Permission denied: {0}")]
14    PermissionDenied(String),
15
16    /// Invalid argument.
17    #[error("Invalid argument: {0}")]
18    InvalidArgument(String),
19
20    /// Invalid working directory.
21    #[error("Invalid working directory: {0}")]
22    InvalidWorkingDirectory(String),
23
24    /// Command execution failed with exit code.
25    #[error("Command failed with exit code {code}: {stderr}")]
26    ExecutionFailed {
27        /// Exit code returned by the command.
28        code: i32,
29        /// Standard error output from the command.
30        stderr: String,
31    },
32
33    /// Command timed out.
34    #[error("Command timed out after {0:?}")]
35    Timeout(std::time::Duration),
36
37    /// IO error.
38    #[error("IO error: {0}")]
39    IoError(#[from] std::io::Error),
40
41    /// Environment error.
42    #[error("Environment error: {0}")]
43    EnvironmentError(String),
44
45    /// Encoding error.
46    #[error("Encoding error: {0}")]
47    EncodingError(String),
48
49    /// Validation error.
50    #[error("Validation error: {0}")]
51    ValidationError(String),
52
53    /// Pipeline error.
54    #[error("Pipeline error: {0}")]
55    PipelineError(String),
56
57    /// Pipeline is empty (no commands).
58    #[error("Pipeline has no commands")]
59    PipelineEmpty,
60
61    /// Pipeline stdin connection error.
62    #[error("Failed to connect pipeline stdin: {0}")]
63    PipelineStdinError(String),
64
65    /// Pipeline stdout connection error.
66    #[error("Failed to connect pipeline stdout: {0}")]
67    PipelineStdoutError(String),
68
69    /// Argument validation error.
70    #[error("Argument validation error: {0}")]
71    ArgumentError(String),
72
73    /// Child process error.
74    #[error("Child process error: {0}")]
75    ChildError(String),
76
77    /// Shell execution error.
78    #[error("Shell execution error: {0}")]
79    ShellError(String),
80
81    /// Generic error.
82    #[error("Error: {0}")]
83    Other(String),
84}
85
86/// Result type for script command operations.
87pub type ScriptResult<T> = Result<T, ScriptError>;
88
89impl From<std::process::Output> for ScriptError {
90    fn from(output: std::process::Output) -> Self {
91        let code = output.status.code().unwrap_or(-1);
92        let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
93        ScriptError::ExecutionFailed { code, stderr }
94    }
95}