neo3/neo_clients/
errors.rs

1use std::{error::Error, fmt::Debug, sync::Arc};
2
3use crate::{crypto::CryptoError, neo_clients::JsonRpcError, TypeError};
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7/// An error thrown when making a call to the provider
8pub enum ProviderError {
9	/// An error during NNS name resolution
10	#[error("nns name not found: {0}")]
11	NnsError(String),
12	/// Invalid reverse NNS name
13	#[error("reverse nns name not pointing to itself: {0}")]
14	NnsNotOwned(String),
15	/// Error in underlying lib `serde_json`
16	#[error(transparent)]
17	SerdeJson(#[from] serde_json::Error),
18	/// Error in underlying lib `hex`
19	#[error(transparent)]
20	HexError(#[from] hex::FromHexError),
21	/// Error in underlying lib `reqwest`
22	#[error(transparent)]
23	HTTPError(#[from] Arc<reqwest::Error>),
24	/// Reponse error
25	#[error(transparent)]
26	JsonRpcError(#[from] JsonRpcError),
27	/// Custom error from unknown source
28	#[error("custom error: {0}")]
29	CustomError(String),
30	/// RPC method is not supported by this provider
31	#[error("unsupported RPC")]
32	UnsupportedRPC,
33	/// Node is not supported by this provider
34	#[error("unsupported node client")]
35	UnsupportedNodeClient,
36	/// Signer is not available to this provider.
37	#[error("Attempted to sign a transaction with no available signer. Hint: did you mean to use a SignerMiddleware?"
38    )]
39	SignerUnavailable,
40	#[error("Illegal state: {0}")]
41	IllegalState(String),
42	#[error("Invalid address")]
43	InvalidAddress,
44	#[error(transparent)]
45	CryptoError(#[from] CryptoError),
46	#[error(transparent)]
47	TypeError(#[from] TypeError),
48	#[error("Invalid password")]
49	InvalidPassword,
50	/// Error parsing data
51	#[error("Parse error: {0}")]
52	ParseError(String),
53	/// Error locking a mutex
54	#[error("Lock error")]
55	LockError,
56	/// Protocol not found
57	#[error("Protocol not found")]
58	ProtocolNotFound,
59	/// Network not found
60	#[error("Network not found")]
61	NetworkNotFound,
62}
63
64impl PartialEq for ProviderError {
65	fn eq(&self, other: &Self) -> bool {
66		match (self, other) {
67			(ProviderError::SerdeJson(a), ProviderError::SerdeJson(b)) =>
68				a.to_string() == b.to_string(),
69			(ProviderError::HTTPError(a), ProviderError::HTTPError(b)) => a.status() == b.status(),
70			(ProviderError::CustomError(a), ProviderError::CustomError(b)) => a == b,
71			(ProviderError::UnsupportedRPC, ProviderError::UnsupportedRPC) => true,
72			(ProviderError::UnsupportedNodeClient, ProviderError::UnsupportedNodeClient) => true,
73			(ProviderError::SignerUnavailable, ProviderError::SignerUnavailable) => true,
74			(ProviderError::IllegalState(a), ProviderError::IllegalState(b)) => a == b,
75			(ProviderError::InvalidAddress, ProviderError::InvalidAddress) => true,
76			(ProviderError::CryptoError(a), ProviderError::CryptoError(b)) => a == b,
77			(ProviderError::TypeError(a), ProviderError::TypeError(b)) => a == b,
78			(ProviderError::InvalidPassword, ProviderError::InvalidPassword) => true,
79			_ => false,
80		}
81	}
82}
83
84// Implementing Clone manually for `ProviderError`
85impl Clone for ProviderError {
86	fn clone(&self) -> Self {
87		match self {
88			ProviderError::NnsError(message) => ProviderError::NnsError(message.clone()),
89			ProviderError::NnsNotOwned(message) => ProviderError::NnsNotOwned(message.clone()),
90			ProviderError::SerdeJson(error) => ProviderError::SerdeJson(serde_json::Error::io(
91				std::io::Error::new(std::io::ErrorKind::Other, error.to_string()),
92			)),
93			ProviderError::HexError(error) => ProviderError::HexError(error.clone()),
94			ProviderError::HTTPError(error) => ProviderError::HTTPError(Arc::clone(error)),
95			ProviderError::JsonRpcError(error) => ProviderError::JsonRpcError(error.clone()),
96			ProviderError::CustomError(message) => ProviderError::CustomError(message.clone()),
97			ProviderError::UnsupportedRPC => ProviderError::UnsupportedRPC,
98			ProviderError::UnsupportedNodeClient => ProviderError::UnsupportedNodeClient,
99			ProviderError::SignerUnavailable => ProviderError::SignerUnavailable,
100			ProviderError::IllegalState(message) => ProviderError::IllegalState(message.clone()),
101			ProviderError::InvalidAddress => ProviderError::InvalidAddress,
102			ProviderError::CryptoError(error) => ProviderError::CryptoError(error.clone()),
103			ProviderError::TypeError(error) => ProviderError::TypeError(error.clone()),
104			ProviderError::InvalidPassword => ProviderError::InvalidPassword,
105			ProviderError::ParseError(message) => ProviderError::ParseError(message.clone()),
106			ProviderError::LockError => ProviderError::LockError,
107			ProviderError::ProtocolNotFound => ProviderError::ProtocolNotFound,
108			ProviderError::NetworkNotFound => ProviderError::NetworkNotFound,
109		}
110	}
111}