1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum ShellTunnelError {
8 #[error("session not found: {0}")]
10 SessionNotFound(String),
11
12 #[error("session already exists: {0}")]
14 SessionExists(String),
15
16 #[error("invalid state transition from {from:?} to {to:?}")]
18 InvalidStateTransition {
19 from: crate::session::SessionState,
20 to: crate::session::SessionState,
21 },
22
23 #[error("PTY error: {0}")]
25 Pty(String),
26
27 #[error("I/O error: {0}")]
29 Io(#[from] std::io::Error),
30
31 #[error("command execution timeout")]
33 Timeout,
34
35 #[error("session terminated")]
37 SessionTerminated,
38
39 #[error("internal lock poisoned")]
41 LockPoisoned,
42
43 #[error("channel send error: {0}")]
45 ChannelSend(String),
46
47 #[error("channel closed")]
49 ChannelClosed,
50
51 #[error("command execution failed: {0}")]
53 ExecutionFailed(String),
54
55 #[error("output parse error: {0}")]
57 ParseError(String),
58
59 #[error("session not executable: current state is {0:?}")]
61 NotExecutable(crate::session::SessionState),
62
63 #[cfg(feature = "tls")]
65 #[error("tls error: {0}")]
66 Tls(String),
67
68 #[error("tunnel error: {0}")]
70 Tunnel(String),
71
72 #[cfg(feature = "self-update")]
74 #[error("update error: {0}")]
75 Update(String),
76}
77
78pub 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}