moonproof_node_client/
error.rs1use crate::event::SubmissionStage;
9
10#[derive(Debug, thiserror::Error)]
14pub enum NodeClientError {
15 #[error("transaction submission failed: {message}")]
18 Submission {
19 message: String,
20 tx_data: Vec<u8>,
21 #[source]
22 source: Option<Box<dyn std::error::Error + Send + Sync>>,
23 },
24
25 #[error("connection error: {message}")]
28 Connection {
29 message: String,
30 #[source]
31 source: Option<Box<dyn std::error::Error + Send + Sync>>,
32 },
33
34 #[error(
38 "transaction did not reach desired stage '{desired_stage}' and no other error was reported"
39 )]
40 TransactionProgress {
41 message: String,
42 tx_data: Vec<u8>,
43 desired_stage: SubmissionStage,
44 },
45
46 #[error("failed to parse node response: {message}")]
50 Parse {
51 message: String,
52 #[source]
53 source: Option<Box<dyn std::error::Error + Send + Sync>>,
54 },
55
56 #[error("transaction usurped (replaced in pool by another matching tx)")]
60 TransactionUsurped { tx_data: Vec<u8> },
61
62 #[error("transaction dropped from mempool (pool likely full / congested)")]
65 TransactionDropped { tx_data: Vec<u8> },
66
67 #[error("transaction rejected as invalid by node")]
70 TransactionInvalid {
71 tx_data: Vec<u8>,
72 #[source]
73 source: Option<Box<dyn std::error::Error + Send + Sync>>,
74 },
75}
76
77impl NodeClientError {
78 #[must_use]
82 pub fn tx_data(&self) -> Option<&[u8]> {
83 match self {
84 Self::Submission { tx_data, .. }
85 | Self::TransactionProgress { tx_data, .. }
86 | Self::TransactionUsurped { tx_data }
87 | Self::TransactionDropped { tx_data }
88 | Self::TransactionInvalid { tx_data, .. } => Some(tx_data),
89 Self::Connection { .. } | Self::Parse { .. } => None,
90 }
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn error_variants_display_is_informative() {
100 let err = NodeClientError::TransactionProgress {
101 message: "stream ended".into(),
102 tx_data: vec![1, 2, 3],
103 desired_stage: SubmissionStage::Finalized,
104 };
105 let rendered = format!("{err}");
106 assert!(
107 rendered.contains("Finalized"),
108 "progress error should name the desired stage: {rendered}"
109 );
110 }
111
112 #[test]
113 fn tx_data_accessor_returns_bytes_for_tx_scoped_variants() {
114 let tx = vec![0xaa; 32];
115 let err = NodeClientError::TransactionInvalid {
116 tx_data: tx.clone(),
117 source: None,
118 };
119 assert_eq!(err.tx_data(), Some(tx.as_slice()));
120 }
121
122 #[test]
123 fn tx_data_accessor_returns_none_for_transport_variants() {
124 let err = NodeClientError::Connection {
125 message: "ws down".into(),
126 source: None,
127 };
128 assert_eq!(err.tx_data(), None);
129
130 let err = NodeClientError::Parse {
131 message: "bad u64".into(),
132 source: None,
133 };
134 assert_eq!(err.tx_data(), None);
135 }
136
137 #[test]
138 fn node_client_error_is_send_sync() {
139 fn assert_send_sync<T: Send + Sync>() {}
140 assert_send_sync::<NodeClientError>();
141 }
142}