use thiserror::Error;
#[derive(Debug, Error)]
pub enum EventError {
#[error("Failed to publish event: {0}")]
PublishError(String),
#[error("Handler '{handler}' failed: {message}")]
HandlerError {
handler: String,
message: String,
},
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Event not found: {0}")]
NotFound(String),
#[error("Event bus is closed")]
BusClosed,
#[error("Channel send error: {0}")]
ChannelError(String),
#[error("Timeout waiting for event")]
Timeout,
#[error("Invalid event type: {0}")]
InvalidEventType(String),
#[error("Handler already registered: {0}")]
DuplicateHandler(String),
}
impl EventError {
pub fn handler(handler: impl Into<String>, message: impl Into<String>) -> Self {
Self::HandlerError {
handler: handler.into(),
message: message.into(),
}
}
pub fn publish(message: impl Into<String>) -> Self {
Self::PublishError(message.into())
}
pub fn serialization(message: impl Into<String>) -> Self {
Self::SerializationError(message.into())
}
}
impl From<tokio::sync::broadcast::error::SendError<String>> for EventError {
fn from(e: tokio::sync::broadcast::error::SendError<String>) -> Self {
Self::ChannelError(e.to_string())
}
}
impl From<serde_json::Error> for EventError {
fn from(e: serde_json::Error) -> Self {
Self::SerializationError(e.to_string())
}
}