bitcoind_async_client/
error.rs1use std::fmt;
3
4use bitcoin::Network;
5use serde::{Deserialize, Serialize};
6use serde_json::Error as SerdeJsonError;
7use thiserror::Error;
8
9#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum ClientError {
12 #[error("Missing username or password")]
14 MissingUserPassword,
15
16 #[error("RPC server returned error '{1}' (code {0})")]
25 Server(i32, String),
26
27 #[error("Error parsing rpc response: {0}")]
28 Parse(String),
29
30 #[error("Could not create RPC Param")]
32 Param(String),
33
34 #[error("{0}")]
36 Body(String),
37
38 #[error("Obtained failure status({0}): {1}")]
40 Status(String, String),
41
42 #[error("Malformed Response: {0}")]
44 MalformedResponse(String),
45
46 #[error("Could not connect: {0}")]
48 Connection(String),
49
50 #[error("Timeout")]
52 Timeout,
53
54 #[error("HttpRedirect: {0}")]
56 HttpRedirect(String),
57
58 #[error("Could not build request: {0}")]
60 ReqBuilder(String),
61
62 #[error("Max retries {0} exceeded")]
64 MaxRetriesExceeded(u8),
65
66 #[error("Could not create request: {0}")]
68 Request(String),
69
70 #[error("Network address: {0}")]
72 WrongNetworkAddress(Network),
73
74 #[error(transparent)]
76 UnexpectedServerVersion(#[from] UnexpectedServerVersionError),
77
78 #[error(transparent)]
80 Sign(#[from] SignRawTransactionWithWalletError),
81
82 #[error("Could not get xpriv from wallet")]
84 Xpriv,
85
86 #[error("{0}")]
88 Other(String),
89}
90
91impl ClientError {
92 pub fn is_tx_not_found(&self) -> bool {
93 matches!(self, Self::Server(-5, _))
94 }
95
96 pub fn is_block_not_found(&self) -> bool {
97 matches!(self, Self::Server(-5, _))
98 }
99
100 pub fn is_missing_or_invalid_input(&self) -> bool {
101 matches!(self, Self::Server(-26, _)) || matches!(self, Self::Server(-25, _))
102 }
103}
104
105impl From<SerdeJsonError> for ClientError {
106 fn from(value: SerdeJsonError) -> Self {
107 Self::Parse(format!("Could not parse {value}"))
108 }
109}
110
111#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
113pub struct BitcoinRpcError {
114 pub code: i32,
115 pub message: String,
116}
117
118impl fmt::Display for BitcoinRpcError {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 write!(f, "RPC error {}: {}", self.code, self.message)
121 }
122}
123
124impl From<BitcoinRpcError> for ClientError {
125 fn from(value: BitcoinRpcError) -> Self {
126 Self::Server(value.code, value.message)
127 }
128}
129
130#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
132pub struct SignRawTransactionWithWalletError {
133 txid: String,
135 vout: u32,
137 #[serde(rename = "scriptSig")]
139 script_sig: String,
140 sequence: u32,
142 error: String,
144}
145
146impl fmt::Display for SignRawTransactionWithWalletError {
147 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
148 write!(
149 f,
150 "error signing raw transaction with wallet: {}",
151 self.error
152 )
153 }
154}
155
156#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
158pub struct UnexpectedServerVersionError {
159 pub got: usize,
161 pub expected: Vec<usize>,
163}
164
165impl fmt::Display for UnexpectedServerVersionError {
166 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
167 let mut expected = String::new();
168 for version in &self.expected {
169 let v = format!(" {version} ");
170 expected.push_str(&v);
171 }
172 write!(
173 f,
174 "unexpected bitcoind version, got: {} expected one of: {}",
175 self.got, expected
176 )
177 }
178}