Skip to content

Commit

Permalink
blockbook: use thiserror annotations to generate Display implemen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
bachmannscode authored and Michael Bachmann committed Jun 12, 2023
1 parent fdf1927 commit 5ccf2ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 37 deletions.
11 changes: 2 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,15 @@ pub mod websocket;
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// An error during a network request.
#[error("error occured during the network request: {0}")]
RequestError(reqwest::Error),
/// An error while parsing a URL.
#[error("invalid url: {0}")]
UrlError(url::ParseError),
}

type Result<T> = std::result::Result<T, Error>;

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::RequestError(e) => e.fmt(f),
Error::UrlError(e) => e.fmt(f),
}
}
}

impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
Self::RequestError(e)
Expand Down
32 changes: 4 additions & 28 deletions src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,19 @@ use futures::{SinkExt, StreamExt};
#[non_exhaustive]
pub enum Error {
/// An unexpected response format was encountered.
#[error("the json-rpc data field doesn't match the object expected based on its id")]
DataObjectMismatch,
/// Blockbook indicated an unsuccessful subscription attempt.
#[error("the server did not establish your subscription")]
SubscriptionFailed,
/// The WebSocket connection was closed.
#[error("the websocket connection got closed; reinstantiate the client to reconnect")]
WebsocketClosed,
/// A WebSocket error.
#[error("the websocket connection experienced a fatal error: {0:?}\nreinstantiate the client to reconnect")]
WebsocketError(std::sync::Arc<tokio_tungstenite::tungstenite::Error>),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::DataObjectMismatch => {
write!(
f,
"the json-rpc data field doesn't match the object expected based on its id"
)
}
Self::SubscriptionFailed => {
write!(f, "the server did not establish your subscription")
}
Self::WebsocketClosed => {
write!(
f,
"the websocket connection got closed; reinstantiate the client to reconnect"
)
}
Self::WebsocketError(e) => {
write!(
f,
"the websocket connection experienced a fatal error: {e:?}\nreinstantiate the client to reconnect"
)
}
}
}
}

type Result<T> = std::result::Result<T, Error>;

/// Information about the full node backing the Blockbook server and the chain state.
Expand Down

0 comments on commit 5ccf2ba

Please # to comment.