Skip to main content

shell_tunnel/
error.rs

1//! Error types for shell-tunnel.
2
3use thiserror::Error;
4
5/// Main error type for shell-tunnel operations.
6#[derive(Error, Debug)]
7pub enum ShellTunnelError {
8    /// Session with the given ID was not found.
9    #[error("session not found: {0}")]
10    SessionNotFound(String),
11
12    /// Session with the given ID already exists.
13    #[error("session already exists: {0}")]
14    SessionExists(String),
15
16    /// Invalid state transition attempted.
17    #[error("invalid state transition from {from:?} to {to:?}")]
18    InvalidStateTransition {
19        from: crate::session::SessionState,
20        to: crate::session::SessionState,
21    },
22
23    /// PTY-related error.
24    #[error("PTY error: {0}")]
25    Pty(String),
26
27    /// I/O error.
28    #[error("I/O error: {0}")]
29    Io(#[from] std::io::Error),
30
31    /// Command execution timeout.
32    #[error("command execution timeout")]
33    Timeout,
34
35    /// Session has been terminated.
36    #[error("session terminated")]
37    SessionTerminated,
38
39    /// Internal lock was poisoned.
40    #[error("internal lock poisoned")]
41    LockPoisoned,
42
43    /// Channel send error.
44    #[error("channel send error: {0}")]
45    ChannelSend(String),
46
47    /// Channel receive error.
48    #[error("channel closed")]
49    ChannelClosed,
50
51    /// Command execution failed.
52    #[error("command execution failed: {0}")]
53    ExecutionFailed(String),
54
55    /// Output parsing error.
56    #[error("output parse error: {0}")]
57    ParseError(String),
58
59    /// Session is not in executable state.
60    #[error("session not executable: current state is {0:?}")]
61    NotExecutable(crate::session::SessionState),
62
63    /// Tunnel (reachability) error.
64    #[error("tunnel error: {0}")]
65    Tunnel(String),
66
67    /// Update error.
68    #[cfg(feature = "self-update")]
69    #[error("update error: {0}")]
70    Update(String),
71}
72
73/// Convenience Result type for shell-tunnel operations.
74pub type Result<T> = std::result::Result<T, ShellTunnelError>;
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_session_not_found_display() {
82        let err = ShellTunnelError::SessionNotFound("sess-00000001".into());
83        assert!(err.to_string().contains("sess-00000001"));
84        assert!(err.to_string().contains("not found"));
85    }
86
87    #[test]
88    fn test_session_exists_display() {
89        let err = ShellTunnelError::SessionExists("sess-00000002".into());
90        assert!(err.to_string().contains("sess-00000002"));
91        assert!(err.to_string().contains("already exists"));
92    }
93
94    #[test]
95    fn test_io_error_conversion() {
96        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
97        let shell_err: ShellTunnelError = io_err.into();
98        assert!(matches!(shell_err, ShellTunnelError::Io(_)));
99        assert!(shell_err.to_string().contains("I/O error"));
100    }
101
102    #[test]
103    fn test_timeout_display() {
104        let err = ShellTunnelError::Timeout;
105        assert!(err.to_string().contains("timeout"));
106    }
107
108    #[test]
109    fn test_pty_error_display() {
110        let err = ShellTunnelError::Pty("failed to spawn".into());
111        assert!(err.to_string().contains("PTY error"));
112        assert!(err.to_string().contains("failed to spawn"));
113    }
114}