Skip to main content

claude_wrapper/
error.rs

1use std::path::PathBuf;
2
3/// Errors returned by claude-wrapper operations.
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6    /// The `claude` binary was not found in PATH.
7    #[error("claude binary not found in PATH")]
8    NotFound,
9
10    /// A claude command failed with a non-zero exit code.
11    #[error("claude command failed: {command} (exit code {exit_code}){}{}{}", working_dir.as_ref().map(|d| format!(" (in {})", d.display())).unwrap_or_default(), if stdout.is_empty() { String::new() } else { format!("\nstdout: {stdout}") }, if stderr.is_empty() { String::new() } else { format!("\nstderr: {stderr}") })]
12    CommandFailed {
13        command: String,
14        exit_code: i32,
15        stdout: String,
16        stderr: String,
17        working_dir: Option<PathBuf>,
18    },
19
20    /// An I/O error occurred while spawning or communicating with the process.
21    #[error("io error: {message}{}", working_dir.as_ref().map(|d| format!(" (in {})", d.display())).unwrap_or_default())]
22    Io {
23        message: String,
24        #[source]
25        source: std::io::Error,
26        working_dir: Option<PathBuf>,
27    },
28
29    /// The command timed out.
30    #[error("claude command timed out after {timeout_seconds}s")]
31    Timeout { timeout_seconds: u64 },
32
33    /// JSON parsing failed.
34    #[cfg(feature = "json")]
35    #[error("json parse error: {message}")]
36    Json {
37        message: String,
38        #[source]
39        source: serde_json::Error,
40    },
41
42    /// The installed CLI version does not meet the minimum requirement.
43    #[error("CLI version {found} does not meet minimum requirement {minimum}")]
44    VersionMismatch {
45        found: crate::version::CliVersion,
46        minimum: crate::version::CliVersion,
47    },
48}
49
50impl From<std::io::Error> for Error {
51    fn from(e: std::io::Error) -> Self {
52        Self::Io {
53            message: e.to_string(),
54            source: e,
55            working_dir: None,
56        }
57    }
58}
59
60/// Result type alias for claude-wrapper operations.
61pub type Result<T> = std::result::Result<T, Error>;
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    fn command_failed(stdout: &str, stderr: &str, working_dir: Option<PathBuf>) -> Error {
68        Error::CommandFailed {
69            command: "/bin/claude --print".to_string(),
70            exit_code: 7,
71            stdout: stdout.to_string(),
72            stderr: stderr.to_string(),
73            working_dir,
74        }
75    }
76
77    #[test]
78    fn command_failed_display_includes_command_and_exit_code() {
79        let e = command_failed("", "", None);
80        let s = e.to_string();
81        assert!(s.contains("/bin/claude --print"));
82        assert!(s.contains("exit code 7"));
83    }
84
85    #[test]
86    fn command_failed_display_omits_empty_stdout_and_stderr() {
87        let s = command_failed("", "", None).to_string();
88        assert!(!s.contains("stdout:"));
89        assert!(!s.contains("stderr:"));
90    }
91
92    #[test]
93    fn command_failed_display_includes_nonempty_stdout() {
94        let s = command_failed("hello", "", None).to_string();
95        assert!(s.contains("stdout: hello"));
96    }
97
98    #[test]
99    fn command_failed_display_includes_nonempty_stderr() {
100        let s = command_failed("", "boom", None).to_string();
101        assert!(s.contains("stderr: boom"));
102    }
103
104    #[test]
105    fn command_failed_display_includes_both_streams_when_present() {
106        let s = command_failed("out", "err", None).to_string();
107        assert!(s.contains("stdout: out"));
108        assert!(s.contains("stderr: err"));
109    }
110
111    #[test]
112    fn command_failed_display_includes_working_dir_when_present() {
113        let s = command_failed("", "", Some(PathBuf::from("/tmp/proj"))).to_string();
114        assert!(s.contains("/tmp/proj"));
115    }
116
117    #[test]
118    fn command_failed_display_omits_working_dir_when_absent() {
119        let s = command_failed("", "", None).to_string();
120        assert!(!s.contains("(in "));
121    }
122
123    #[test]
124    fn timeout_display_formats_seconds() {
125        let s = Error::Timeout {
126            timeout_seconds: 42,
127        }
128        .to_string();
129        assert!(s.contains("42s"));
130    }
131
132    #[test]
133    fn io_error_display_includes_working_dir_when_present() {
134        let e = Error::Io {
135            message: "spawn failed".to_string(),
136            source: std::io::Error::new(std::io::ErrorKind::NotFound, "no file"),
137            working_dir: Some(PathBuf::from("/work")),
138        };
139        let s = e.to_string();
140        assert!(s.contains("spawn failed"));
141        assert!(s.contains("/work"));
142    }
143}