use crate::event::SubmissionStage;
#[derive(Debug, thiserror::Error)]
pub enum NodeClientError {
#[error("transaction submission failed: {message}")]
Submission {
message: String,
tx_data: Vec<u8>,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
#[error("connection error: {message}")]
Connection {
message: String,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
#[error(
"transaction did not reach desired stage '{desired_stage}' and no other error was reported"
)]
TransactionProgress {
message: String,
tx_data: Vec<u8>,
desired_stage: SubmissionStage,
},
#[error("failed to parse node response: {message}")]
Parse {
message: String,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
#[error("transaction usurped (replaced in pool by another matching tx)")]
TransactionUsurped { tx_data: Vec<u8> },
#[error("transaction dropped from mempool (pool likely full / congested)")]
TransactionDropped { tx_data: Vec<u8> },
#[error("transaction rejected as invalid by node")]
TransactionInvalid {
tx_data: Vec<u8>,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
}
impl NodeClientError {
#[must_use]
pub fn tx_data(&self) -> Option<&[u8]> {
match self {
Self::Submission { tx_data, .. }
| Self::TransactionProgress { tx_data, .. }
| Self::TransactionUsurped { tx_data }
| Self::TransactionDropped { tx_data }
| Self::TransactionInvalid { tx_data, .. } => Some(tx_data),
Self::Connection { .. } | Self::Parse { .. } => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_variants_display_is_informative() {
let err = NodeClientError::TransactionProgress {
message: "stream ended".into(),
tx_data: vec![1, 2, 3],
desired_stage: SubmissionStage::Finalized,
};
let rendered = format!("{err}");
assert!(
rendered.contains("Finalized"),
"progress error should name the desired stage: {rendered}"
);
}
#[test]
fn tx_data_accessor_returns_bytes_for_tx_scoped_variants() {
let tx = vec![0xaa; 32];
let err = NodeClientError::TransactionInvalid {
tx_data: tx.clone(),
source: None,
};
assert_eq!(err.tx_data(), Some(tx.as_slice()));
}
#[test]
fn tx_data_accessor_returns_none_for_transport_variants() {
let err = NodeClientError::Connection {
message: "ws down".into(),
source: None,
};
assert_eq!(err.tx_data(), None);
let err = NodeClientError::Parse {
message: "bad u64".into(),
source: None,
};
assert_eq!(err.tx_data(), None);
}
#[test]
fn node_client_error_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<NodeClientError>();
}
}