bitcoincore_rpc_async/
error.rs

1// To the extent possible under law, the author(s) have dedicated all
2// copyright and related and neighboring rights to this software to
3// the public domain worldwide. This software is distributed without
4// any warranty.
5//
6// You should have received a copy of the CC0 Public Domain Dedication
7// along with this software.
8// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
9//
10
11use std::{error, fmt, io};
12
13use super::bitcoin;
14use bitcoin::hashes::hex;
15use bitcoin::secp256k1;
16use jsonrpc_async as jsonrpc;
17use serde_json;
18
19/// The error type for errors produced in this library.
20#[derive(Debug)]
21pub enum Error {
22    JsonRpc(jsonrpc::error::Error),
23    Hex(hex::Error),
24    Json(serde_json::error::Error),
25    BitcoinSerialization(bitcoin::consensus::encode::Error),
26    Secp256k1(secp256k1::Error),
27    Io(io::Error),
28    InvalidAmount(bitcoin::util::amount::ParseAmountError),
29    InvalidCookieFile,
30    /// The JSON result had an unexpected structure.
31    UnexpectedStructure,
32}
33
34impl From<jsonrpc::error::Error> for Error {
35    fn from(e: jsonrpc::error::Error) -> Error {
36        Error::JsonRpc(e)
37    }
38}
39
40impl From<hex::Error> for Error {
41    fn from(e: hex::Error) -> Error {
42        Error::Hex(e)
43    }
44}
45
46impl From<serde_json::error::Error> for Error {
47    fn from(e: serde_json::error::Error) -> Error {
48        Error::Json(e)
49    }
50}
51
52impl From<bitcoin::consensus::encode::Error> for Error {
53    fn from(e: bitcoin::consensus::encode::Error) -> Error {
54        Error::BitcoinSerialization(e)
55    }
56}
57
58impl From<secp256k1::Error> for Error {
59    fn from(e: secp256k1::Error) -> Error {
60        Error::Secp256k1(e)
61    }
62}
63
64impl From<io::Error> for Error {
65    fn from(e: io::Error) -> Error {
66        Error::Io(e)
67    }
68}
69
70impl From<bitcoin::util::amount::ParseAmountError> for Error {
71    fn from(e: bitcoin::util::amount::ParseAmountError) -> Error {
72        Error::InvalidAmount(e)
73    }
74}
75
76impl fmt::Display for Error {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        match *self {
79            Error::JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
80            Error::Hex(ref e) => write!(f, "hex decode error: {}", e),
81            Error::Json(ref e) => write!(f, "JSON error: {}", e),
82            Error::BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
83            Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
84            Error::Io(ref e) => write!(f, "I/O error: {}", e),
85            Error::InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
86            Error::InvalidCookieFile => write!(f, "invalid cookie file"),
87            Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
88        }
89    }
90}
91
92impl error::Error for Error {
93    fn description(&self) -> &str {
94        "bitcoincore-rpc error"
95    }
96
97    fn cause(&self) -> Option<&dyn error::Error> {
98        match *self {
99            Error::JsonRpc(ref e) => Some(e),
100            Error::Hex(ref e) => Some(e),
101            Error::Json(ref e) => Some(e),
102            Error::BitcoinSerialization(ref e) => Some(e),
103            Error::Secp256k1(ref e) => Some(e),
104            Error::Io(ref e) => Some(e),
105            _ => None,
106        }
107    }
108}