moonpool_transport/rpc/
reply_error.rs1use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub enum ReplyError {
17 BrokenPromise,
22
23 ConnectionFailed,
27
28 Timeout,
32
33 Serialization {
37 message: String,
39 },
40
41 EndpointNotFound,
45}
46
47impl std::fmt::Display for ReplyError {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 match self {
50 ReplyError::BrokenPromise => write!(f, "server dropped promise without reply"),
51 ReplyError::ConnectionFailed => write!(f, "connection failed"),
52 ReplyError::Timeout => write!(f, "request timed out"),
53 ReplyError::Serialization { message } => write!(f, "serialization error: {}", message),
54 ReplyError::EndpointNotFound => write!(f, "endpoint not found"),
55 }
56 }
57}
58
59impl std::error::Error for ReplyError {}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_reply_error_display() {
67 assert_eq!(
68 ReplyError::BrokenPromise.to_string(),
69 "server dropped promise without reply"
70 );
71 assert_eq!(
72 ReplyError::ConnectionFailed.to_string(),
73 "connection failed"
74 );
75 assert_eq!(ReplyError::Timeout.to_string(), "request timed out");
76 assert_eq!(
77 ReplyError::Serialization {
78 message: "invalid JSON".to_string()
79 }
80 .to_string(),
81 "serialization error: invalid JSON"
82 );
83 assert_eq!(
84 ReplyError::EndpointNotFound.to_string(),
85 "endpoint not found"
86 );
87 }
88
89 #[test]
90 fn test_reply_error_serde_roundtrip() {
91 let errors = vec![
92 ReplyError::BrokenPromise,
93 ReplyError::ConnectionFailed,
94 ReplyError::Timeout,
95 ReplyError::Serialization {
96 message: "test error".to_string(),
97 },
98 ReplyError::EndpointNotFound,
99 ];
100
101 for error in errors {
102 let json = serde_json::to_string(&error).expect("serialize");
103 let decoded: ReplyError = serde_json::from_str(&json).expect("deserialize");
104 assert_eq!(error, decoded);
105 }
106 }
107
108 #[test]
109 fn test_reply_error_is_error_trait() {
110 let error: Box<dyn std::error::Error> = Box::new(ReplyError::BrokenPromise);
111 assert!(error.to_string().contains("promise"));
112 }
113}