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    /// TLS configuration error.
64    #[cfg(feature = "tls")]
65    #[error("tls error: {0}")]
66    Tls(String),
67
68    /// Tunnel (reachability) error.
69    #[error("tunnel error: {0}")]
70    Tunnel(String),
71
72    /// Update error.
73    #[cfg(feature = "self-update")]
74    #[error("update error: {0}")]
75    Update(String),
76}
77
78/// Convenience Result type for shell-tunnel operations.
79pub type Result<T> = std::result::Result<T, ShellTunnelError>;
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_session_not_found_display() {
87        let err = ShellTunnelError::SessionNotFound("sess-00000001".into());
88        assert!(err.to_string().contains("sess-00000001"));
89        assert!(err.to_string().contains("not found"));
90    }
91
92    #[test]
93    fn test_session_exists_display() {
94        let err = ShellTunnelError::SessionExists("sess-00000002".into());
95        assert!(err.to_string().contains("sess-00000002"));
96        assert!(err.to_string().contains("already exists"));
97    }
98
99    #[test]
100    fn test_io_error_conversion() {
101        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
102        let shell_err: ShellTunnelError = io_err.into();
103        assert!(matches!(shell_err, ShellTunnelError::Io(_)));
104        assert!(shell_err.to_string().contains("I/O error"));
105    }
106
107    #[test]
108    fn test_timeout_display() {
109        let err = ShellTunnelError::Timeout;
110        assert!(err.to_string().contains("timeout"));
111    }
112
113    #[test]
114    fn test_pty_error_display() {
115        let err = ShellTunnelError::Pty("failed to spawn".into());
116        assert!(err.to_string().contains("PTY error"));
117        assert!(err.to_string().contains("failed to spawn"));
118    }
119}