Skip to main content

bitcoin_zmq/
errors.rs

1use std::{fmt, sync::mpsc};
2
3use bytes::Bytes;
4use futures::sync::mpsc as fmpsc;
5use futures_zmq::Error as ZMQError;
6
7use super::Topic;
8
9/// Errors caused by bitcoind
10#[derive(Debug)]
11pub enum BitcoinError {
12    /// Topic is missing
13    MissingTopic,
14    /// Payload is missing
15    MissingPayload,
16    /// Unexpected topic
17    UnexpectedTopic,
18}
19
20impl fmt::Display for BitcoinError {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        let printable = match self {
23            BitcoinError::MissingTopic => "missing topic",
24            BitcoinError::MissingPayload => "missing payload",
25            BitcoinError::UnexpectedTopic => "unexpected topic",
26        };
27        write!(f, "{}", printable)
28    }
29}
30
31/// Primary error type concerning the ZMQ subscription
32#[derive(Debug)]
33pub enum SubscriptionError {
34    /// Error originating from bitcoind
35    Bitcoin(BitcoinError),
36    /// Error sending over the broadcast channel
37    BroadcastChannel(mpsc::SendError<(Topic, Bytes)>),
38    /// Error sending over single stream channel
39    Channel(fmpsc::SendError<Vec<u8>>),
40    /// Error in the connection to bitcoind
41    Connection(ZMQError),
42}
43
44impl fmt::Display for SubscriptionError {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        match self {
47            SubscriptionError::Bitcoin(err) => err.fmt(f),
48            SubscriptionError::BroadcastChannel(err) => err.fmt(f),
49            SubscriptionError::Channel(err) => err.fmt(f),
50            SubscriptionError::Connection(err) => err.fmt(f),
51        }
52    }
53}
54
55impl From<BitcoinError> for SubscriptionError {
56    fn from(err: BitcoinError) -> SubscriptionError {
57        SubscriptionError::Bitcoin(err)
58    }
59}
60
61impl From<mpsc::SendError<(Topic, Bytes)>> for SubscriptionError {
62    fn from(err: mpsc::SendError<(Topic, Bytes)>) -> SubscriptionError {
63        SubscriptionError::BroadcastChannel(err)
64    }
65}
66
67impl From<fmpsc::SendError<Vec<u8>>> for SubscriptionError {
68    fn from(err: fmpsc::SendError<Vec<u8>>) -> SubscriptionError {
69        SubscriptionError::Channel(err)
70    }
71}
72
73impl From<ZMQError> for SubscriptionError {
74    fn from(err: ZMQError) -> SubscriptionError {
75        SubscriptionError::Connection(err)
76    }
77}