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}")]
41 Status(u16, String),
42
43 #[error("Malformed Response: {0}")]
45 MalformedResponse(String),
46
47 #[error("Could not connect: {0}")]
49 Connection(String),
50
51 #[error("Timeout")]
53 Timeout,
54
55 #[error("HttpRedirect: {0}")]
57 HttpRedirect(String),
58
59 #[error("Could not build request: {0}")]
61 ReqBuilder(String),
62
63 #[error("Max retries {0} exceeded")]
65 MaxRetriesExceeded(u8),
66
67 #[error("Could not create request: {0}")]
69 Request(String),
70
71 #[error("Network address: {0}")]
73 WrongNetworkAddress(Network),
74
75 #[error(transparent)]
77 UnexpectedServerVersion(#[from] UnexpectedServerVersionError),
78
79 #[error(transparent)]
81 Sign(#[from] SignRawTransactionWithWalletError),
82
83 #[error("Could not get xpriv from wallet")]
85 Xpriv,
86
87 #[error("{0}")]
89 Other(String),
90}
91
92impl ClientError {
93 pub fn is_tx_not_found(&self) -> bool {
94 matches!(self, Self::Server(-5, _))
95 }
96
97 pub fn is_block_not_found(&self) -> bool {
98 matches!(self, Self::Server(-5, _))
99 }
100
101 pub fn is_missing_or_invalid_input(&self) -> bool {
102 matches!(self, Self::Server(-26, _)) || matches!(self, Self::Server(-25, _))
103 }
104}
105
106impl From<SerdeJsonError> for ClientError {
107 fn from(value: SerdeJsonError) -> Self {
108 Self::Parse(format!("Could not parse {value}"))
109 }
110}
111
112#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
114pub struct BitcoinRpcError {
115 pub code: i32,
116 pub message: String,
117}
118
119impl fmt::Display for BitcoinRpcError {
120 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121 write!(f, "RPC error {}: {}", self.code, self.message)
122 }
123}
124
125impl From<BitcoinRpcError> for ClientError {
126 fn from(value: BitcoinRpcError) -> Self {
127 Self::Server(value.code, value.message)
128 }
129}
130
131#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
133pub struct SignRawTransactionWithWalletError {
134 txid: String,
136 vout: u32,
138 #[serde(rename = "scriptSig")]
140 script_sig: String,
141 sequence: u32,
143 error: String,
145}
146
147impl fmt::Display for SignRawTransactionWithWalletError {
148 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149 write!(
150 f,
151 "error signing raw transaction with wallet: {}",
152 self.error
153 )
154 }
155}
156
157#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
159pub struct UnexpectedServerVersionError {
160 pub got: usize,
162 pub expected: Vec<usize>,
164}
165
166impl fmt::Display for UnexpectedServerVersionError {
167 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168 let mut expected = String::new();
169 for version in &self.expected {
170 let v = format!(" {version} ");
171 expected.push_str(&v);
172 }
173 write!(
174 f,
175 "unexpected bitcoind version, got: {} expected one of: {}",
176 self.got, expected
177 )
178 }
179}