use feagi_io::FeagiNetworkError;
use feagi_structures::FeagiDataError;
use std::error::Error;
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone)]
pub enum FeagiAgentError {
InitFail(String),
ConnectionFailed(String),
AuthenticationFailed(String),
UnableToDecodeReceivedData(String),
UnableToSendData(String),
SocketFailure(String),
Other(String),
}
impl Display for FeagiAgentError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
FeagiAgentError::InitFail(msg) => {
write!(f, "FeagiAgentError: Init failed: {}", msg)
}
FeagiAgentError::ConnectionFailed(msg) => {
write!(f, "FeagiAgentError: Connection failed: {}", msg)
}
FeagiAgentError::AuthenticationFailed(msg) => {
write!(f, "FeagiAgentError: Authentication failed: {}", msg)
}
FeagiAgentError::UnableToDecodeReceivedData(msg) => {
write!(
f,
"FeagiAgentError: Unable to decode received data: {}",
msg
)
}
FeagiAgentError::UnableToSendData(msg) => {
write!(f, "FeagiAgentError: Unable to send data: {}", msg)
}
FeagiAgentError::SocketFailure(msg) => {
write!(f, "FeagiAgentError: Socket failure: {}", msg)
}
FeagiAgentError::Other(msg) => {
write!(f, "FeagiAgentError: {}", msg)
}
}
}
}
impl Error for FeagiAgentError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
impl From<FeagiDataError> for FeagiAgentError {
fn from(err: FeagiDataError) -> Self {
match err {
FeagiDataError::DeserializationError(msg) => {
FeagiAgentError::UnableToDecodeReceivedData(msg)
}
FeagiDataError::SerializationError(msg) => FeagiAgentError::UnableToSendData(msg),
FeagiDataError::BadParameters(msg) => {
FeagiAgentError::Other(format!("Bad parameters: {}", msg))
}
FeagiDataError::NeuronError(msg) => {
FeagiAgentError::Other(format!("Neuron error: {}", msg))
}
FeagiDataError::InternalError(msg) => {
FeagiAgentError::Other(format!("Internal error: {}", msg))
}
FeagiDataError::ResourceLockedWhileRunning(msg) => {
FeagiAgentError::Other(format!("Resource locked: {}", msg))
}
FeagiDataError::ConstError(msg) => {
FeagiAgentError::Other(format!("Const error: {}", msg))
}
FeagiDataError::NotImplemented => FeagiAgentError::Other("Not implemented".to_string()),
}
}
}
impl From<FeagiNetworkError> for FeagiAgentError {
fn from(err: FeagiNetworkError) -> Self {
match err {
FeagiNetworkError::CannotBind(msg) => {
FeagiAgentError::InitFail(format!("Cannot bind: {}", msg))
}
FeagiNetworkError::CannotUnbind(msg) => {
FeagiAgentError::SocketFailure(format!("Cannot unbind: {}", msg))
}
FeagiNetworkError::CannotConnect(msg) => {
FeagiAgentError::ConnectionFailed(format!("Cannot connect: {}", msg))
}
FeagiNetworkError::CannotDisconnect(msg) => {
FeagiAgentError::SocketFailure(format!("Cannot disconnect: {}", msg))
}
FeagiNetworkError::SendFailed(msg) => FeagiAgentError::UnableToSendData(msg),
FeagiNetworkError::ReceiveFailed(msg) => {
FeagiAgentError::UnableToDecodeReceivedData(format!("Receive failed: {}", msg))
}
FeagiNetworkError::InvalidSocketProperties(msg) => {
FeagiAgentError::InitFail(format!("Invalid socket properties: {}", msg))
}
FeagiNetworkError::SocketCreationFailed(msg) => {
FeagiAgentError::SocketFailure(format!("Socket creation failed: {}", msg))
}
FeagiNetworkError::GeneralFailure(msg) => {
FeagiAgentError::Other(format!("General failure: {}", msg))
}
}
}
}