webterm_agent/models/
agent_error.rs

1use flatbuffers::InvalidFlatbuffer;
2use std::fmt;
3use tokio::sync::broadcast::error::RecvError;
4use tokio::sync::mpsc::error::SendError;
5use tokio_tungstenite::tungstenite;
6use tokio_tungstenite::tungstenite::Bytes;
7use webterm_core::models::reader_socket_error::ReaderSocketError;
8use webterm_core::models::webterm_error::WebtermError;
9use webterm_core::types::{ActivityId, FrontendId, SessionId};
10
11#[derive(Debug)]
12pub enum AgentError {
13    RuntimeError(String),
14    FlatbuffersError(InvalidFlatbuffer),
15    FBParseError(String),
16    SocketError(tungstenite::Error),
17    SocketClosed,
18    SocketSendError(SendError<Bytes>),
19    SocketRecvError(RecvError),
20    SocketReadError(ReaderSocketError),
21    PtyProcessError(pty_process::Error),
22    IOError(std::io::Error),
23    URLParseError(url::ParseError),
24    CoreError(WebtermError),
25    FrontendNotFound(Option<FrontendId>),
26    SessionNotFound(Option<SessionId>),
27    ActivityNotFound(Option<ActivityId>),
28    ReqwestError(reqwest::Error),
29    RelayErrorAgentAlreadyExists,
30}
31
32impl std::error::Error for AgentError {}
33
34impl fmt::Display for AgentError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            AgentError::RuntimeError(e) => write!(f, "Runtime error: {}", e),
38            AgentError::FlatbuffersError(e) => write!(f, "Flatbuffers error: {}", e),
39            AgentError::FBParseError(e) => write!(f, "Flatbuffers parse error: {}", e),
40            AgentError::SocketError(e) => write!(f, "Socket error: {}", e),
41            AgentError::SocketClosed => write!(f, "Socket closed"),
42            AgentError::SocketSendError(e) => write!(f, "Socket send error: {}", e),
43            AgentError::SocketRecvError(e) => write!(f, "Socket receive error: {}", e),
44            AgentError::SocketReadError(e) => write!(f, "Socket read error: {}", e),
45            AgentError::PtyProcessError(e) => write!(f, "Pty process error: {}", e),
46            AgentError::IOError(e) => write!(f, "IO error: {}", e),
47            AgentError::URLParseError(e) => write!(f, "URL parse error: {}", e),
48            AgentError::CoreError(e) => write!(f, "CoreError: {}", e),
49            AgentError::FrontendNotFound(e) => write!(f, "Frontend not found: {:?}", e),
50            AgentError::SessionNotFound(e) => write!(f, "Session not found: {:?}", e),
51            AgentError::ActivityNotFound(e) => write!(f, "Activity not found: {:?}", e),
52            AgentError::ReqwestError(e) => write!(f, "Reqwest error: {}", e),
53            AgentError::RelayErrorAgentAlreadyExists => write!(f, "Agent already exists"),
54        }
55    }
56}
57
58impl From<InvalidFlatbuffer> for AgentError {
59    fn from(err: InvalidFlatbuffer) -> Self {
60        AgentError::FlatbuffersError(err)
61    }
62}
63
64impl From<SendError<Bytes>> for AgentError {
65    fn from(err: SendError<Bytes>) -> Self {
66        AgentError::SocketSendError(err)
67    }
68}
69
70impl From<RecvError> for AgentError {
71    fn from(err: RecvError) -> Self {
72        AgentError::SocketRecvError(err)
73    }
74}
75
76impl From<ReaderSocketError> for AgentError {
77    fn from(err: ReaderSocketError) -> Self {
78        AgentError::SocketReadError(err)
79    }
80}
81
82impl From<tungstenite::Error> for AgentError {
83    fn from(err: tungstenite::Error) -> Self {
84        AgentError::SocketError(err)
85    }
86}
87
88impl From<pty_process::Error> for AgentError {
89    fn from(err: pty_process::Error) -> Self {
90        AgentError::PtyProcessError(err)
91    }
92}
93
94impl From<std::io::Error> for AgentError {
95    fn from(err: std::io::Error) -> Self {
96        AgentError::IOError(err)
97    }
98}
99
100impl From<url::ParseError> for AgentError {
101    fn from(err: url::ParseError) -> Self {
102        AgentError::URLParseError(err)
103    }
104}
105
106impl From<WebtermError> for AgentError {
107    fn from(err: WebtermError) -> Self {
108        AgentError::CoreError(err)
109    }
110}
111
112impl From<reqwest::Error> for AgentError {
113    fn from(err: reqwest::Error) -> Self {
114        AgentError::ReqwestError(err)
115    }
116}