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 #[error("tunnel error: {0}")]
65 Tunnel(String),
66
67 #[cfg(feature = "self-update")]
69 #[error("update error: {0}")]
70 Update(String),
71}
72
73pub 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}