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 = "self-update")]
65 #[error("update error: {0}")]
66 Update(String),
67}
68
69pub 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}