use thiserror::Error;
pub type Result<T> = std::result::Result<T, RabbitMeshError>;
#[derive(Error, Debug)]
pub enum RabbitMeshError {
#[error("AMQP connection error: {0}")]
Connection(#[from] lapin::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("RPC call timed out after {timeout_ms}ms")]
Timeout { timeout_ms: u64 },
#[error("Service '{service_name}' not found")]
ServiceNotFound { service_name: String },
#[error("Method '{method_name}' not found in service '{service_name}'")]
MethodNotFound {
service_name: String,
method_name: String,
},
#[error("Invalid message format: {reason}")]
InvalidMessage { reason: String },
#[error("Service handler error: {0}")]
Handler(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
#[error("Join error: {0}")]
Join(#[from] tokio::task::JoinError),
}
impl RabbitMeshError {
pub fn handler_error<T: ToString>(message: T) -> Self {
Self::Handler(message.to_string())
}
pub fn config_error<T: ToString>(message: T) -> Self {
Self::Config(message.to_string())
}
pub fn internal_error<T: ToString>(message: T) -> Self {
Self::Internal(message.to_string())
}
pub fn is_recoverable(&self) -> bool {
match self {
Self::Connection(_) => true,
Self::Timeout { .. } => true,
Self::Io(_) => true,
_ => false,
}
}
}