Skip to content

Commit e7917f3

Browse files
committed
feat(error)!: Add Error::Model variant
Previously, each RPC method that called `.into_model()` produced a distinct, version-specific error variant (`GetBlockVerboseOne`, `GetBlockHeaderVerbose`, `GetBlockFilter`), requiring version-gated imports and the potential of future API churn. Remove those variants and consolidate all `.into_model()` errors into a single `Error::Model` variant. A `pub(crate) fn model<E>(e: E) -> Self` constructor is added to `Error` to keep call sites clean.
1 parent 7ea5862 commit e7917f3

4 files changed

Lines changed: 22 additions & 38 deletions

File tree

src/client.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Client {
183183
pub fn get_block_filter(&self, block_hash: &BlockHash) -> Result<GetBlockFilter, Error> {
184184
let block_filter: v30::GetBlockFilter =
185185
self.call("getblockfilter", &[json!(block_hash)])?;
186-
block_filter.into_model().map_err(Error::GetBlockFilter)
186+
block_filter.into_model().map_err(Error::model)
187187
}
188188

189189
/// Retrieves the `Header` for a `Block` given its `BlockHash`.
@@ -245,9 +245,7 @@ impl Client {
245245
) -> Result<GetBlockHeaderVerbose, Error> {
246246
let header_info: v30::GetBlockHeaderVerbose =
247247
self.call("getblockheader", &[json!(block_hash)])?;
248-
header_info
249-
.into_model()
250-
.map_err(Error::GetBlockHeaderVerbose)
248+
header_info.into_model().map_err(Error::model)
251249
}
252250

253251
/// Retrieves the verbose JSON representation of a block (verbosity 1).
@@ -262,7 +260,7 @@ impl Client {
262260
pub fn get_block_verbose(&self, block_hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
263261
let block_info: v30::GetBlockVerboseOne =
264262
self.call("getblock", &[json!(block_hash), json!(1)])?;
265-
block_info.into_model().map_err(Error::GetBlockVerboseOne)
263+
block_info.into_model().map_err(Error::model)
266264
}
267265
}
268266

src/client/v28.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ impl Client {
2727
) -> Result<GetBlockHeaderVerbose, Error> {
2828
let header_info: v28::GetBlockHeaderVerbose =
2929
self.call("getblockheader", &[json!(block_hash)])?;
30-
header_info
31-
.into_model()
32-
.map_err(Error::GetBlockHeaderVerbose)
30+
header_info.into_model().map_err(Error::model)
3331
}
3432

3533
/// Retrieves the verbose JSON representation of a block (verbosity 1).
@@ -44,6 +42,6 @@ impl Client {
4442
pub fn get_block_verbose(&self, block_hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
4543
let block_info: v28::GetBlockVerboseOne =
4644
self.call("getblock", &[json!(block_hash), json!(1)])?;
47-
block_info.into_model().map_err(Error::GetBlockVerboseOne)
45+
block_info.into_model().map_err(Error::model)
4846
}
4947
}

src/error.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,17 @@ use core::num::TryFromIntError;
77
use std::io;
88

99
use bitcoin::{consensus::encode::FromHexError, hex::HexToArrayError};
10-
#[cfg(feature = "28_0")]
11-
use corepc_types::v17::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
12-
#[cfg(not(feature = "28_0"))]
13-
use corepc_types::v30::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
14-
use corepc_types::{bitcoin, v30::GetBlockFilterError};
10+
use corepc_types::bitcoin;
1511
use jsonrpc::serde_json;
1612

17-
/// Result type alias for the RPC client.
18-
pub type Result<T> = std::result::Result<T, Error>;
19-
2013
/// Errors that can occur when using the Bitcoin RPC client.
2114
#[derive(Debug)]
2215
pub enum Error {
2316
/// Hex deserialization error
2417
DecodeHex(FromHexError),
2518

26-
/// Error converting `GetBlockVersboseOne` type into the model type
27-
GetBlockVerboseOne(GetBlockVerboseOneError),
28-
29-
/// Error modeling [`GetBlockHeaderVerbose`](corepc_types::model::GetBlockHeaderVerbose).
30-
GetBlockHeaderVerbose(GetBlockHeaderVerboseError),
31-
32-
/// Error modeling [`GetBlockFilter`](corepc_types::model::GetBlockFilter)
33-
GetBlockFilter(GetBlockFilterError),
19+
/// Error converting a version-specific RPC type into the model type.
20+
Model(Box<dyn core::error::Error + Send + Sync + 'static>),
3421

3522
/// Invalid or corrupted cookie file.
3623
InvalidCookieFile,
@@ -58,9 +45,7 @@ impl fmt::Display for Error {
5845
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5946
match self {
6047
Error::DecodeHex(e) => write!(f, "hex deserialization error: {e}"),
61-
Error::GetBlockVerboseOne(e) => write!(f, "block verbose error: {e}"),
62-
Error::GetBlockHeaderVerbose(e) => write!(f, "block header verbose error: {e}"),
63-
Error::GetBlockFilter(e) => write!(f, "block filter error: {e}"),
48+
Error::Model(e) => write!(f, "model conversion error: {e}"),
6449
Error::InvalidCookieFile => write!(f, "invalid or missing cookie file"),
6550
Error::InvalidUrl(e) => write!(f, "invalid RPC URL: {e}"),
6651
Error::HexToArray(e) => write!(f, "hash parsing error: {e}"),
@@ -74,6 +59,16 @@ impl fmt::Display for Error {
7459

7560
impl core::error::Error for Error {}
7661

62+
impl Error {
63+
/// Converts `e` to a [`Error::Model`] error.
64+
pub(crate) fn model<E>(e: E) -> Self
65+
where
66+
E: core::error::Error + Send + Sync + 'static,
67+
{
68+
Self::Model(Box::new(e))
69+
}
70+
}
71+
7772
// Conversions from other error types
7873
impl From<jsonrpc::Error> for Error {
7974
fn from(e: jsonrpc::Error) -> Self {
@@ -105,12 +100,6 @@ impl From<TryFromIntError> for Error {
105100
}
106101
}
107102

108-
impl From<GetBlockVerboseOneError> for Error {
109-
fn from(e: GetBlockVerboseOneError) -> Self {
110-
Error::GetBlockVerboseOne(e)
111-
}
112-
}
113-
114103
impl From<FromHexError> for Error {
115104
fn from(e: FromHexError) -> Self {
116105
Error::DecodeHex(e)

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
mod client;
1010
mod error;
1111

12-
pub use client::{Auth, Client};
13-
pub use error::{Error, Result};
12+
pub use client::*;
13+
pub use error::*;
1414

15-
pub use jsonrpc;
16-
// Re-export corepc_types
1715
pub use corepc_types;
16+
pub use jsonrpc;

0 commit comments

Comments
 (0)