corepc_client/client_sync/
error.rs

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