Skip to content

Commit 19dab53

Browse files
committed
Create client error module
Most of the errors are shared between the sync async clients. Move them to a common module.
1 parent c3017f4 commit 19dab53

4 files changed

Lines changed: 130 additions & 226 deletions

File tree

client/src/bdk_client/error.rs

Lines changed: 8 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// SPDX-License-Identifier: CC0-1.0
22

3-
use std::{error, fmt, io};
4-
5-
use bitcoin::hex;
63
use types::v17::{
74
GetBlockHeaderError, GetBlockHeaderVerboseError, GetBlockVerboseOneError,
85
GetRawTransactionVerboseError,
@@ -13,137 +10,32 @@ use types::v29::{
1310
GetBlockVerboseOneError as GetBlockVerboseOneErrorV29,
1411
};
1512

16-
/// The error type for errors produced in this library.
17-
#[derive(Debug)]
18-
pub enum Error {
19-
JsonRpc(jsonrpc::error::Error),
20-
HexToArray(hex::HexToArrayError),
21-
HexToBytes(hex::HexToBytesError),
22-
Json(serde_json::error::Error),
23-
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
24-
Io(io::Error),
25-
InvalidCookieFile,
26-
/// The JSON result had an unexpected structure.
27-
UnexpectedStructure,
28-
/// The daemon returned an error string.
29-
Returned(String),
30-
/// The server version did not match what was expected.
31-
ServerVersion(UnexpectedServerVersionError),
32-
/// Missing user/password.
33-
MissingUserPassword,
34-
}
35-
36-
impl From<jsonrpc::error::Error> for Error {
37-
fn from(e: jsonrpc::error::Error) -> Error { Error::JsonRpc(e) }
38-
}
39-
40-
impl From<hex::HexToArrayError> for Error {
41-
fn from(e: hex::HexToArrayError) -> Self { Self::HexToArray(e) }
42-
}
43-
44-
impl From<hex::HexToBytesError> for Error {
45-
fn from(e: hex::HexToBytesError) -> Self { Self::HexToBytes(e) }
46-
}
47-
48-
impl From<serde_json::error::Error> for Error {
49-
fn from(e: serde_json::error::Error) -> Error { Error::Json(e) }
50-
}
51-
52-
impl From<bitcoin::consensus::encode::FromHexError> for Error {
53-
fn from(e: bitcoin::consensus::encode::FromHexError) -> Error { Error::BitcoinSerialization(e) }
54-
}
55-
56-
impl From<io::Error> for Error {
57-
fn from(e: io::Error) -> Error { Error::Io(e) }
58-
}
13+
pub use crate::error::{Error, UnexpectedServerVersionError};
5914

6015
impl From<GetBlockHeaderError> for Error {
61-
fn from(e: GetBlockHeaderError) -> Self { Self::Returned(e.to_string()) }
16+
fn from(e: GetBlockHeaderError) -> Self { Self::Model(e.to_string()) }
6217
}
6318

6419
impl From<GetBlockHeaderVerboseError> for Error {
65-
fn from(e: GetBlockHeaderVerboseError) -> Self { Self::Returned(e.to_string()) }
20+
fn from(e: GetBlockHeaderVerboseError) -> Self { Self::Model(e.to_string()) }
6621
}
6722

6823
impl From<GetBlockVerboseOneError> for Error {
69-
fn from(e: GetBlockVerboseOneError) -> Self { Self::Returned(e.to_string()) }
24+
fn from(e: GetBlockVerboseOneError) -> Self { Self::Model(e.to_string()) }
7025
}
7126

7227
impl From<GetRawTransactionVerboseError> for Error {
73-
fn from(e: GetRawTransactionVerboseError) -> Self { Self::Returned(e.to_string()) }
28+
fn from(e: GetRawTransactionVerboseError) -> Self { Self::Model(e.to_string()) }
7429
}
7530

7631
impl From<GetBlockHeaderVerboseErrorV29> for Error {
77-
fn from(e: GetBlockHeaderVerboseErrorV29) -> Self { Self::Returned(e.to_string()) }
32+
fn from(e: GetBlockHeaderVerboseErrorV29) -> Self { Self::Model(e.to_string()) }
7833
}
7934

8035
impl From<GetBlockVerboseOneErrorV29> for Error {
81-
fn from(e: GetBlockVerboseOneErrorV29) -> Self { Self::Returned(e.to_string()) }
36+
fn from(e: GetBlockVerboseOneErrorV29) -> Self { Self::Model(e.to_string()) }
8237
}
8338

8439
impl From<GetBlockFilterError> for Error {
85-
fn from(e: GetBlockFilterError) -> Self { Self::Returned(e.to_string()) }
86-
}
87-
88-
impl fmt::Display for Error {
89-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90-
use Error::*;
91-
92-
match *self {
93-
JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
94-
HexToArray(ref e) => write!(f, "hex to array decode error: {}", e),
95-
HexToBytes(ref e) => write!(f, "hex to bytes decode error: {}", e),
96-
Json(ref e) => write!(f, "JSON error: {}", e),
97-
BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
98-
Io(ref e) => write!(f, "I/O error: {}", e),
99-
InvalidCookieFile => write!(f, "invalid cookie file"),
100-
UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
101-
Returned(ref s) => write!(f, "the daemon returned an error string: {}", s),
102-
ServerVersion(ref e) => write!(f, "server version: {}", e),
103-
MissingUserPassword => write!(f, "missing user and/or password"),
104-
}
105-
}
106-
}
107-
108-
impl error::Error for Error {
109-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
110-
use Error::*;
111-
112-
match *self {
113-
JsonRpc(ref e) => Some(e),
114-
HexToArray(ref e) => Some(e),
115-
HexToBytes(ref e) => Some(e),
116-
Json(ref e) => Some(e),
117-
BitcoinSerialization(ref e) => Some(e),
118-
Io(ref e) => Some(e),
119-
ServerVersion(ref e) => Some(e),
120-
InvalidCookieFile | UnexpectedStructure | Returned(_) | MissingUserPassword => None,
121-
}
122-
}
123-
}
124-
125-
/// Error returned when RPC client expects a different version than bitcoind reports.
126-
#[derive(Debug, Clone, PartialEq, Eq)]
127-
pub struct UnexpectedServerVersionError {
128-
/// Version from server.
129-
pub got: usize,
130-
/// Expected server version.
131-
pub expected: Vec<usize>,
132-
}
133-
134-
impl fmt::Display for UnexpectedServerVersionError {
135-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136-
let mut expected = String::new();
137-
for version in &self.expected {
138-
let v = format!(" {} ", version);
139-
expected.push_str(&v);
140-
}
141-
write!(f, "unexpected bitcoind version, got: {} expected one of: {}", self.got, expected)
142-
}
143-
}
144-
145-
impl error::Error for UnexpectedServerVersionError {}
146-
147-
impl From<UnexpectedServerVersionError> for Error {
148-
fn from(e: UnexpectedServerVersionError) -> Self { Self::ServerVersion(e) }
40+
fn from(e: GetBlockFilterError) -> Self { Self::Model(e.to_string()) }
14941
}

client/src/client_sync/error.rs

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,3 @@
11
// SPDX-License-Identifier: CC0-1.0
22

3-
use std::{error, fmt, io};
4-
5-
use bitcoin::hex;
6-
7-
/// The error type for errors produced in this library.
8-
#[derive(Debug)]
9-
pub enum Error {
10-
JsonRpc(jsonrpc::error::Error),
11-
HexToArray(hex::HexToArrayError),
12-
HexToBytes(hex::HexToBytesError),
13-
Json(serde_json::error::Error),
14-
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
15-
Io(io::Error),
16-
InvalidCookieFile,
17-
/// The JSON result had an unexpected structure.
18-
UnexpectedStructure,
19-
/// The daemon returned an error string.
20-
Returned(String),
21-
/// The server version did not match what was expected.
22-
ServerVersion(UnexpectedServerVersionError),
23-
/// Missing user/password.
24-
MissingUserPassword,
25-
}
26-
27-
impl From<jsonrpc::error::Error> for Error {
28-
fn from(e: jsonrpc::error::Error) -> Error { Error::JsonRpc(e) }
29-
}
30-
31-
impl From<hex::HexToArrayError> for Error {
32-
fn from(e: hex::HexToArrayError) -> Self { Self::HexToArray(e) }
33-
}
34-
35-
impl From<hex::HexToBytesError> for Error {
36-
fn from(e: hex::HexToBytesError) -> Self { Self::HexToBytes(e) }
37-
}
38-
39-
impl From<serde_json::error::Error> for Error {
40-
fn from(e: serde_json::error::Error) -> Error { Error::Json(e) }
41-
}
42-
43-
impl From<bitcoin::consensus::encode::FromHexError> for Error {
44-
fn from(e: bitcoin::consensus::encode::FromHexError) -> Error { Error::BitcoinSerialization(e) }
45-
}
46-
47-
impl From<io::Error> for Error {
48-
fn from(e: io::Error) -> Error { Error::Io(e) }
49-
}
50-
51-
impl fmt::Display for Error {
52-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53-
use Error::*;
54-
55-
match *self {
56-
JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
57-
HexToArray(ref e) => write!(f, "hex to array decode error: {}", e),
58-
HexToBytes(ref e) => write!(f, "hex to bytes decode error: {}", e),
59-
Json(ref e) => write!(f, "JSON error: {}", e),
60-
BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
61-
Io(ref e) => write!(f, "I/O error: {}", e),
62-
InvalidCookieFile => write!(f, "invalid cookie file"),
63-
UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
64-
Returned(ref s) => write!(f, "the daemon returned an error string: {}", s),
65-
ServerVersion(ref e) => write!(f, "server version: {}", e),
66-
MissingUserPassword => write!(f, "missing user and/or password"),
67-
}
68-
}
69-
}
70-
71-
impl error::Error for Error {
72-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
73-
use Error::*;
74-
75-
match *self {
76-
JsonRpc(ref e) => Some(e),
77-
HexToArray(ref e) => Some(e),
78-
HexToBytes(ref e) => Some(e),
79-
Json(ref e) => Some(e),
80-
BitcoinSerialization(ref e) => Some(e),
81-
Io(ref e) => Some(e),
82-
ServerVersion(ref e) => Some(e),
83-
InvalidCookieFile | UnexpectedStructure | Returned(_) | MissingUserPassword => None,
84-
}
85-
}
86-
}
87-
88-
/// Error returned when RPC client expects a different version than bitcoind reports.
89-
#[derive(Debug, Clone, PartialEq, Eq)]
90-
pub struct UnexpectedServerVersionError {
91-
/// Version from server.
92-
pub got: usize,
93-
/// Expected server version.
94-
pub expected: Vec<usize>,
95-
}
96-
97-
impl fmt::Display for UnexpectedServerVersionError {
98-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99-
let mut expected = String::new();
100-
for version in &self.expected {
101-
let v = format!(" {} ", version);
102-
expected.push_str(&v);
103-
}
104-
write!(f, "unexpected bitcoind version, got: {} expected one of: {}", self.got, expected)
105-
}
106-
}
107-
108-
impl error::Error for UnexpectedServerVersionError {}
109-
110-
impl From<UnexpectedServerVersionError> for Error {
111-
fn from(e: UnexpectedServerVersionError) -> Self { Self::ServerVersion(e) }
112-
}
3+
pub use crate::error::{Error, UnexpectedServerVersionError};

client/src/error.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Error types shared between the sync and async JSON-RPC clients.
4+
5+
use std::{error, fmt, io};
6+
7+
use bitcoin::hex;
8+
9+
/// The error type for errors produced in this library.
10+
#[derive(Debug)]
11+
pub enum Error {
12+
JsonRpc(jsonrpc::error::Error),
13+
HexToArray(hex::HexToArrayError),
14+
HexToBytes(hex::HexToBytesError),
15+
Json(serde_json::error::Error),
16+
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
17+
Io(io::Error),
18+
InvalidCookieFile,
19+
/// The JSON result had an unexpected structure.
20+
UnexpectedStructure,
21+
/// The daemon returned an error string.
22+
Returned(String),
23+
/// A model conversion error.
24+
Model(String),
25+
/// The server version did not match what was expected.
26+
ServerVersion(UnexpectedServerVersionError),
27+
/// Missing user/password.
28+
MissingUserPassword,
29+
}
30+
31+
impl From<jsonrpc::error::Error> for Error {
32+
fn from(e: jsonrpc::error::Error) -> Error { Error::JsonRpc(e) }
33+
}
34+
35+
impl From<hex::HexToArrayError> for Error {
36+
fn from(e: hex::HexToArrayError) -> Self { Self::HexToArray(e) }
37+
}
38+
39+
impl From<hex::HexToBytesError> for Error {
40+
fn from(e: hex::HexToBytesError) -> Self { Self::HexToBytes(e) }
41+
}
42+
43+
impl From<serde_json::error::Error> for Error {
44+
fn from(e: serde_json::error::Error) -> Error { Error::Json(e) }
45+
}
46+
47+
impl From<bitcoin::consensus::encode::FromHexError> for Error {
48+
fn from(e: bitcoin::consensus::encode::FromHexError) -> Error { Error::BitcoinSerialization(e) }
49+
}
50+
51+
impl From<io::Error> for Error {
52+
fn from(e: io::Error) -> Error { Error::Io(e) }
53+
}
54+
55+
impl fmt::Display for Error {
56+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57+
use Error::*;
58+
59+
match *self {
60+
JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
61+
HexToArray(ref e) => write!(f, "hex to array decode error: {}", e),
62+
HexToBytes(ref e) => write!(f, "hex to bytes decode error: {}", e),
63+
Json(ref e) => write!(f, "JSON error: {}", e),
64+
BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
65+
Io(ref e) => write!(f, "I/O error: {}", e),
66+
InvalidCookieFile => write!(f, "invalid cookie file"),
67+
UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
68+
Returned(ref s) => write!(f, "the daemon returned an error string: {}", s),
69+
Model(ref s) => write!(f, "model conversion error: {}", s),
70+
ServerVersion(ref e) => write!(f, "server version: {}", e),
71+
MissingUserPassword => write!(f, "missing user and/or password"),
72+
}
73+
}
74+
}
75+
76+
impl error::Error for Error {
77+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
78+
use Error::*;
79+
80+
match *self {
81+
JsonRpc(ref e) => Some(e),
82+
HexToArray(ref e) => Some(e),
83+
HexToBytes(ref e) => Some(e),
84+
Json(ref e) => Some(e),
85+
BitcoinSerialization(ref e) => Some(e),
86+
Io(ref e) => Some(e),
87+
ServerVersion(ref e) => Some(e),
88+
InvalidCookieFile | UnexpectedStructure | Returned(_) | Model(_)
89+
| MissingUserPassword => None,
90+
}
91+
}
92+
}
93+
94+
/// Error returned when RPC client expects a different version than bitcoind reports.
95+
#[derive(Debug, Clone, PartialEq, Eq)]
96+
pub struct UnexpectedServerVersionError {
97+
/// Version from server.
98+
pub got: usize,
99+
/// Expected server version.
100+
pub expected: Vec<usize>,
101+
}
102+
103+
impl fmt::Display for UnexpectedServerVersionError {
104+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105+
let mut expected = String::new();
106+
for version in &self.expected {
107+
let v = format!(" {} ", version);
108+
expected.push_str(&v);
109+
}
110+
write!(f, "unexpected bitcoind version, got: {} expected one of: {}", self.got, expected)
111+
}
112+
}
113+
114+
impl error::Error for UnexpectedServerVersionError {}
115+
116+
impl From<UnexpectedServerVersionError> for Error {
117+
fn from(e: UnexpectedServerVersionError) -> Self { Self::ServerVersion(e) }
118+
}

client/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ pub extern crate bitcoin;
88
/// Re-export the `corepc-types` crate.
99
pub extern crate types;
1010

11+
#[cfg(any(feature = "client-sync", feature = "client-async"))]
12+
pub(crate) mod error;
13+
1114
#[cfg(feature = "client-sync")]
1215
#[macro_use]
1316
pub mod client_sync;

0 commit comments

Comments
 (0)