Skip to main content

moonpool_transport/
error.rs

1//! Error types for the moonpool messaging layer.
2
3use crate::UID;
4
5/// Errors that can occur in the messaging layer.
6#[derive(Debug, thiserror::Error)]
7pub enum MessagingError {
8    /// Endpoint not found for the given token.
9    #[error("endpoint not found: {token}")]
10    EndpointNotFound {
11        /// The token that was not found.
12        token: UID,
13    },
14
15    /// Failed to deserialize a message.
16    #[error("deserialization failed: {message}")]
17    DeserializationFailed {
18        /// Details about the deserialization failure.
19        message: String,
20    },
21
22    /// Failed to serialize a message.
23    #[error("serialization failed: {message}")]
24    SerializationFailed {
25        /// Details about the serialization failure.
26        message: String,
27    },
28
29    /// Peer connection error.
30    #[error("peer error: {message}")]
31    PeerError {
32        /// Details about the peer error.
33        message: String,
34    },
35
36    /// Queue is full and cannot accept more messages.
37    #[error("queue full: capacity {capacity}")]
38    QueueFull {
39        /// Maximum capacity of the queue.
40        capacity: usize,
41    },
42
43    /// Invalid well-known token index.
44    #[error("invalid well-known token: {index} (max: {max})")]
45    InvalidWellKnownToken {
46        /// The invalid token index.
47        index: u32,
48        /// Maximum allowed index.
49        max: usize,
50    },
51
52    /// Transport is closed or shutting down.
53    #[error("transport closed")]
54    TransportClosed,
55
56    /// Invalid state for the requested operation.
57    #[error("invalid state: {message}")]
58    InvalidState {
59        /// Details about the invalid state.
60        message: String,
61    },
62
63    /// Network operation failed.
64    #[error("network error: {message}")]
65    NetworkError {
66        /// Details about the network error.
67        message: String,
68    },
69
70    /// Builder missing required local address.
71    #[error("local_address is required - call .local_address(addr) before .build()")]
72    MissingLocalAddress,
73}
74
75impl From<serde_json::Error> for MessagingError {
76    fn from(err: serde_json::Error) -> Self {
77        MessagingError::DeserializationFailed {
78            message: err.to_string(),
79        }
80    }
81}
82
83impl From<crate::PeerError> for MessagingError {
84    fn from(err: crate::PeerError) -> Self {
85        MessagingError::PeerError {
86            message: err.to_string(),
87        }
88    }
89}