rocketmq_error/unified/
network.rs1use thiserror::Error;
21
22#[derive(Debug, Error)]
24pub enum NetworkError {
25 #[error("Connection failed to {addr}: {reason}")]
27 ConnectionFailed { addr: String, reason: String },
28
29 #[error("Connection timeout to {addr} after {timeout_ms}ms")]
31 ConnectionTimeout { addr: String, timeout_ms: u64 },
32
33 #[error("Connection closed: {addr}")]
35 ConnectionClosed { addr: String },
36
37 #[error("Send failed to {addr}: {reason}")]
39 SendFailed { addr: String, reason: String },
40
41 #[error("Receive failed from {addr}: {reason}")]
43 ReceiveFailed { addr: String, reason: String },
44
45 #[error("Invalid address format: {addr}")]
47 InvalidAddress { addr: String },
48
49 #[error("DNS resolution failed for {host}: {reason}")]
51 DnsResolutionFailed { host: String, reason: String },
52
53 #[error("Too many requests to {addr}, limit: {limit}")]
55 TooManyRequests { addr: String, limit: usize },
56
57 #[error("Request timeout to {addr} after {timeout_ms}ms")]
59 RequestTimeout { addr: String, timeout_ms: u64 },
60}
61
62impl NetworkError {
63 #[inline]
65 pub fn connection_failed(addr: impl Into<String>, reason: impl Into<String>) -> Self {
66 Self::ConnectionFailed {
67 addr: addr.into(),
68 reason: reason.into(),
69 }
70 }
71
72 #[inline]
74 pub fn connection_timeout(addr: impl Into<String>, timeout_ms: u64) -> Self {
75 Self::ConnectionTimeout {
76 addr: addr.into(),
77 timeout_ms,
78 }
79 }
80
81 #[inline]
83 pub fn send_failed(addr: impl Into<String>, reason: impl Into<String>) -> Self {
84 Self::SendFailed {
85 addr: addr.into(),
86 reason: reason.into(),
87 }
88 }
89
90 #[inline]
92 pub fn request_timeout(addr: impl Into<String>, timeout_ms: u64) -> Self {
93 Self::RequestTimeout {
94 addr: addr.into(),
95 timeout_ms,
96 }
97 }
98
99 pub fn addr(&self) -> &str {
101 match self {
102 Self::ConnectionFailed { addr, .. }
103 | Self::ConnectionTimeout { addr, .. }
104 | Self::ConnectionClosed { addr }
105 | Self::SendFailed { addr, .. }
106 | Self::ReceiveFailed { addr, .. }
107 | Self::InvalidAddress { addr }
108 | Self::TooManyRequests { addr, .. }
109 | Self::RequestTimeout { addr, .. } => addr,
110 Self::DnsResolutionFailed { host, .. } => host,
111 }
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn test_network_error_creation() {
121 let err = NetworkError::connection_failed("127.0.0.1:9876", "timeout");
122 assert_eq!(err.addr(), "127.0.0.1:9876");
123 assert!(err.to_string().contains("Connection failed"));
124 }
125
126 #[test]
127 fn test_network_error_display() {
128 let err = NetworkError::ConnectionTimeout {
129 addr: "localhost:10911".to_string(),
130 timeout_ms: 3000,
131 };
132 assert_eq!(
133 err.to_string(),
134 "Connection timeout to localhost:10911 after 3000ms"
135 );
136 }
137}