-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror.rs
More file actions
129 lines (106 loc) · 3.96 KB
/
error.rs
File metadata and controls
129 lines (106 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Error types for the Bitcoin RPC client.
use bitcoin::{consensus::encode::FromHexError, hex::HexToArrayError};
#[cfg(feature = "28_0")]
use corepc_types::v17::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
#[cfg(not(feature = "28_0"))]
use corepc_types::v30::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
use corepc_types::{bitcoin, v30::GetBlockFilterError};
use jsonrpc::serde_json;
use std::{fmt, io, num::TryFromIntError};
/// Result type alias for the RPC client.
pub type Result<T> = std::result::Result<T, Error>;
/// Errors that can occur when using the Bitcoin RPC client.
#[derive(Debug)]
pub enum Error {
/// Hex deserialization error
DecodeHex(FromHexError),
/// Error converting `GetBlockVersboseOne` type into the model type
GetBlockVerboseOne(GetBlockVerboseOneError),
/// Error modeling [`GetBlockHeaderVerbose`](corepc_types::model::GetBlockHeaderVerbose).
GetBlockHeaderVerbose(GetBlockHeaderVerboseError),
/// Error modeling [`GetBlockFilter`](corepc_types::model::GetBlockFilter)
GetBlockFilter(GetBlockFilterError),
/// Invalid or corrupted cookie file.
InvalidCookieFile,
/// The provided URL is syntactically incorrect
InvalidUrl(String),
/// JSON-RPC error from the server.
JsonRpc(jsonrpc::Error),
/// Hash parsing error.
HexToArray(HexToArrayError),
/// JSON serialization/deserialization error.
Json(serde_json::Error),
/// I/O error (e.g., reading cookie file, network issues).
Io(io::Error),
/// Error when converting an integer type to a smaller type due to overflow.
TryFromInt(TryFromIntError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::DecodeHex(e) => write!(f, "hex deserialization error: {e}"),
Error::GetBlockVerboseOne(e) => write!(f, "block verbose error: {e}"),
Error::GetBlockHeaderVerbose(e) => write!(f, "block header verbose error: {e}"),
Error::GetBlockFilter(e) => write!(f, "block filter error: {e}"),
Error::InvalidCookieFile => write!(f, "invalid or missing cookie file"),
Error::InvalidUrl(e) => write!(f, "invalid RPC URL: {e}"),
Error::HexToArray(e) => write!(f, "hash parsing error: {e}"),
Error::JsonRpc(e) => write!(f, "JSON-RPC error: {e}"),
Error::Json(e) => write!(f, "JSON error: {e}"),
Error::Io(e) => write!(f, "I/O error: {e}"),
Error::TryFromInt(e) => write!(f, "integer conversion overflow: {e}"),
}
}
}
impl std::error::Error for Error {}
// Conversions from other error types
impl From<jsonrpc::Error> for Error {
fn from(e: jsonrpc::Error) -> Self {
Error::JsonRpc(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}
impl From<HexToArrayError> for Error {
fn from(e: HexToArrayError) -> Self {
Error::HexToArray(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
impl From<TryFromIntError> for Error {
fn from(e: TryFromIntError) -> Self {
Error::TryFromInt(e)
}
}
impl From<GetBlockVerboseOneError> for Error {
fn from(e: GetBlockVerboseOneError) -> Self {
Error::GetBlockVerboseOne(e)
}
}
impl From<FromHexError> for Error {
fn from(e: FromHexError) -> Self {
Error::DecodeHex(e)
}
}
/// Extension methods for the client error type.
impl Error {
/// Returns `true` if this is a "not found" error returned by `bitcoind`.
///
/// `bitcoind` returns error code `-5` (`RPC_INVALID_ADDRESS_OR_KEY`)
/// whenever a requested block hash, transaction ID, address, or similar object
/// does not exist on the node.
pub fn is_not_found_error(&self) -> bool {
if let Error::JsonRpc(jsonrpc::Error::Rpc(rpc_err)) = self {
rpc_err.code == -5
} else {
false
}
}
}