alloy_network/any/
error.rs1use crate::{Network, TransactionBuilderError};
4use alloy_consensus::error::UnsupportedTransactionType;
5use core::{
6 error::Error,
7 fmt,
8 fmt::{Debug, Display},
9};
10
11pub struct AnyConversionError {
13 inner: Box<dyn Error + Send + Sync + 'static>,
14}
15
16impl AnyConversionError {
17 pub fn new<E>(error: E) -> Self
19 where
20 E: Error + Send + Sync + 'static,
21 {
22 Self { inner: Box::new(error) }
23 }
24
25 pub fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
27 self.inner.as_ref()
28 }
29}
30
31impl fmt::Debug for AnyConversionError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 fmt::Debug::fmt(&self.inner, f)
34 }
35}
36
37impl fmt::Display for AnyConversionError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 fmt::Display::fmt(&self.inner, f)
40 }
41}
42
43impl Error for AnyConversionError {
44 fn source(&self) -> Option<&(dyn Error + 'static)> {
45 self.inner.source()
46 }
47}
48
49impl<N: Network, TxType: Display + Debug + Sync + Send + 'static>
50 From<UnsupportedTransactionType<TxType>> for TransactionBuilderError<N>
51{
52 fn from(value: UnsupportedTransactionType<TxType>) -> Self {
53 Self::Custom(Box::new(value))
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use crate::AnyNetwork;
61 use alloy_consensus::TxType;
62
63 #[test]
64 fn test_tx_builder_error_from_unsupported_tx_type_displays_it() {
65 let error = UnsupportedTransactionType::new(TxType::Eip2930);
66 let error = TransactionBuilderError::<AnyNetwork>::from(error);
67 let actual_msg = error.to_string();
68 let expected_msg = "Unsupported transaction type: EIP-2930";
69
70 assert_eq!(actual_msg, expected_msg);
71 }
72}