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