backbone_messaging/
error.rs1use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum EventError {
8 #[error("Failed to publish event: {0}")]
10 PublishError(String),
11
12 #[error("Handler '{handler}' failed: {message}")]
14 HandlerError {
15 handler: String,
16 message: String,
17 },
18
19 #[error("Serialization error: {0}")]
21 SerializationError(String),
22
23 #[error("Event not found: {0}")]
25 NotFound(String),
26
27 #[error("Event bus is closed")]
29 BusClosed,
30
31 #[error("Channel send error: {0}")]
33 ChannelError(String),
34
35 #[error("Timeout waiting for event")]
37 Timeout,
38
39 #[error("Invalid event type: {0}")]
41 InvalidEventType(String),
42
43 #[error("Handler already registered: {0}")]
45 DuplicateHandler(String),
46}
47
48impl EventError {
49 pub fn handler(handler: impl Into<String>, message: impl Into<String>) -> Self {
51 Self::HandlerError {
52 handler: handler.into(),
53 message: message.into(),
54 }
55 }
56
57 pub fn publish(message: impl Into<String>) -> Self {
59 Self::PublishError(message.into())
60 }
61
62 pub fn serialization(message: impl Into<String>) -> Self {
64 Self::SerializationError(message.into())
65 }
66}
67
68impl From<tokio::sync::broadcast::error::SendError<String>> for EventError {
69 fn from(e: tokio::sync::broadcast::error::SendError<String>) -> Self {
70 Self::ChannelError(e.to_string())
71 }
72}
73
74impl From<serde_json::Error> for EventError {
75 fn from(e: serde_json::Error) -> Self {
76 Self::SerializationError(e.to_string())
77 }
78}