Skip to main content

feagi_agent/
feagi_agent_error.rs

1//! Unified error types for the FEAGI agent (client and server).
2
3use feagi_io::FeagiNetworkError;
4use feagi_structures::FeagiDataError;
5use std::error::Error;
6use std::fmt::{Display, Formatter};
7
8/// Errors that can occur in FEAGI agent operations (both client and server).
9#[derive(Debug, Clone)]
10pub enum FeagiAgentError {
11    /// Unable to initialize/start (typically server-side)
12    InitFail(String),
13    /// Failed to connect
14    ConnectionFailed(String),
15    /// Authentication failed (invalid credentials, expired token, etc.)
16    AuthenticationFailed(String),
17    /// Cannot understand what the remote endpoint sent
18    UnableToDecodeReceivedData(String),
19    /// Failed to send data to the remote endpoint
20    UnableToSendData(String),
21    /// Something went wrong with the server network socket and it should be restarted
22    SocketFailure(String),
23    /// Other/uncategorized error
24    Other(String),
25}
26
27impl Display for FeagiAgentError {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        match self {
30            FeagiAgentError::InitFail(msg) => {
31                write!(f, "FeagiAgentError: Init failed: {}", msg)
32            }
33            FeagiAgentError::ConnectionFailed(msg) => {
34                write!(f, "FeagiAgentError: Connection failed: {}", msg)
35            }
36            FeagiAgentError::AuthenticationFailed(msg) => {
37                write!(f, "FeagiAgentError: Authentication failed: {}", msg)
38            }
39            FeagiAgentError::UnableToDecodeReceivedData(msg) => {
40                write!(
41                    f,
42                    "FeagiAgentError: Unable to decode received data: {}",
43                    msg
44                )
45            }
46            FeagiAgentError::UnableToSendData(msg) => {
47                write!(f, "FeagiAgentError: Unable to send data: {}", msg)
48            }
49            FeagiAgentError::SocketFailure(msg) => {
50                write!(f, "FeagiAgentError: Socket failure: {}", msg)
51            }
52            FeagiAgentError::Other(msg) => {
53                write!(f, "FeagiAgentError: {}", msg)
54            }
55        }
56    }
57}
58
59impl Error for FeagiAgentError {
60    fn source(&self) -> Option<&(dyn Error + 'static)> {
61        None
62    }
63}
64
65impl From<FeagiDataError> for FeagiAgentError {
66    fn from(err: FeagiDataError) -> Self {
67        match err {
68            FeagiDataError::DeserializationError(msg) => {
69                FeagiAgentError::UnableToDecodeReceivedData(msg)
70            }
71            FeagiDataError::SerializationError(msg) => FeagiAgentError::UnableToSendData(msg),
72            FeagiDataError::BadParameters(msg) => {
73                FeagiAgentError::Other(format!("Bad parameters: {}", msg))
74            }
75            FeagiDataError::NeuronError(msg) => {
76                FeagiAgentError::Other(format!("Neuron error: {}", msg))
77            }
78            FeagiDataError::InternalError(msg) => {
79                FeagiAgentError::Other(format!("Internal error: {}", msg))
80            }
81            FeagiDataError::ResourceLockedWhileRunning(msg) => {
82                FeagiAgentError::Other(format!("Resource locked: {}", msg))
83            }
84            FeagiDataError::ConstError(msg) => {
85                FeagiAgentError::Other(format!("Const error: {}", msg))
86            }
87            FeagiDataError::NotImplemented => FeagiAgentError::Other("Not implemented".to_string()),
88        }
89    }
90}
91
92impl From<FeagiNetworkError> for FeagiAgentError {
93    fn from(err: FeagiNetworkError) -> Self {
94        match err {
95            FeagiNetworkError::CannotBind(msg) => {
96                FeagiAgentError::InitFail(format!("Cannot bind: {}", msg))
97            }
98            FeagiNetworkError::CannotUnbind(msg) => {
99                FeagiAgentError::SocketFailure(format!("Cannot unbind: {}", msg))
100            }
101            FeagiNetworkError::CannotConnect(msg) => {
102                FeagiAgentError::ConnectionFailed(format!("Cannot connect: {}", msg))
103            }
104            FeagiNetworkError::CannotDisconnect(msg) => {
105                FeagiAgentError::SocketFailure(format!("Cannot disconnect: {}", msg))
106            }
107            FeagiNetworkError::SendFailed(msg) => FeagiAgentError::UnableToSendData(msg),
108            FeagiNetworkError::ReceiveFailed(msg) => {
109                FeagiAgentError::UnableToDecodeReceivedData(format!("Receive failed: {}", msg))
110            }
111            FeagiNetworkError::InvalidSocketProperties(msg) => {
112                FeagiAgentError::InitFail(format!("Invalid socket properties: {}", msg))
113            }
114            FeagiNetworkError::SocketCreationFailed(msg) => {
115                FeagiAgentError::SocketFailure(format!("Socket creation failed: {}", msg))
116            }
117            FeagiNetworkError::GeneralFailure(msg) => {
118                FeagiAgentError::Other(format!("General failure: {}", msg))
119            }
120        }
121    }
122}