Skip to main content

anycms_event/
error.rs

1//! Error types for the event bus system.
2
3use thiserror::Error;
4
5/// Errors that can occur during event bus operations.
6#[derive(Error, Debug)]
7pub enum EventBusError {
8    /// Failed to publish an event to the bus.
9    #[error("event publish failed for '{event_name}': {reason}")]
10    PublishFailed {
11        /// The event type that failed to publish.
12        event_name: &'static str,
13        /// Why the publish failed.
14        reason: PublishErrorReason,
15    },
16
17    /// A subscriber handler returned an error.
18    #[error("handler error for '{event_name}': {message}")]
19    HandlerError {
20        /// The event type being processed.
21        event_name: String,
22        /// Error message from the handler.
23        message: String,
24    },
25
26    /// The requested topic was not found.
27    #[error("topic not found: {0}")]
28    TopicNotFound(String),
29
30    /// The broadcast channel has been closed.
31    #[error("channel closed for event '{event_name}'")]
32    ChannelClosed {
33        /// The event type whose channel closed.
34        event_name: String,
35    },
36
37    /// An error occurred in the underlying transport layer.
38    #[error("transport error: {message}")]
39    TransportError {
40        /// Transport error message.
41        message: String,
42    },
43
44    /// Failed to downcast a type-erased event back to its concrete type.
45    #[error("downcast failed for event '{event_name}'")]
46    DowncastFailed {
47        /// The expected event type.
48        event_name: String,
49    },
50}
51
52/// Specific reasons a publish operation can fail.
53#[derive(Debug)]
54pub enum PublishErrorReason {
55    /// The broadcast channel is full or closed.
56    ChannelError(String),
57    /// A serialization error (used only by transport layers).
58    SerializationError(String),
59}
60
61impl std::fmt::Display for PublishErrorReason {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        match self {
64            PublishErrorReason::ChannelError(s) => write!(f, "channel error: {}", s),
65            PublishErrorReason::SerializationError(s) => write!(f, "serialization error: {}", s),
66        }
67    }
68}
69
70/// Convenience alias for results using [`EventBusError`].
71pub type Result<T> = std::result::Result<T, EventBusError>;