1use std::{error, fmt, io};
12
13use crate::bitcoin;
14use crate::bitcoin::hashes::hex;
15use crate::bitcoin::secp256k1;
16use jsonrpc;
17use serde_json;
18
19#[derive(Debug)]
21pub enum Error {
22 JsonRpc(jsonrpc::error::Error),
23 Hex(hex::HexToBytesError),
24 Json(serde_json::error::Error),
25 BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
26 Secp256k1(secp256k1::Error),
27 Io(io::Error),
28 InvalidAmount(bitcoin::amount::ParseAmountError),
29 InvalidCookieFile,
30 UnexpectedStructure,
32 ReturnedError(String),
34}
35
36impl From<jsonrpc::error::Error> for Error {
37 fn from(e: jsonrpc::error::Error) -> Error {
38 Error::JsonRpc(e)
39 }
40}
41
42impl From<hex::HexToBytesError> for Error {
43 fn from(e: hex::HexToBytesError) -> Error {
44 Error::Hex(e)
45 }
46}
47
48impl From<serde_json::error::Error> for Error {
49 fn from(e: serde_json::error::Error) -> Error {
50 Error::Json(e)
51 }
52}
53
54impl From<bitcoin::consensus::encode::FromHexError> for Error {
55 fn from(e: bitcoin::consensus::encode::FromHexError) -> Error {
56 Error::BitcoinSerialization(e)
57 }
58}
59
60impl From<secp256k1::Error> for Error {
61 fn from(e: secp256k1::Error) -> Error {
62 Error::Secp256k1(e)
63 }
64}
65
66impl From<io::Error> for Error {
67 fn from(e: io::Error) -> Error {
68 Error::Io(e)
69 }
70}
71
72impl From<bitcoin::amount::ParseAmountError> for Error {
73 fn from(e: bitcoin::amount::ParseAmountError) -> Error {
74 Error::InvalidAmount(e)
75 }
76}
77
78impl fmt::Display for Error {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match *self {
81 Error::JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
82 Error::Hex(ref e) => write!(f, "hex decode error: {}", e),
83 Error::Json(ref e) => write!(f, "JSON error: {}", e),
84 Error::BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
85 Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
86 Error::Io(ref e) => write!(f, "I/O error: {}", e),
87 Error::InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
88 Error::InvalidCookieFile => write!(f, "invalid cookie file"),
89 Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
90 Error::ReturnedError(ref s) => write!(f, "the daemon returned an error string: {}", s),
91 }
92 }
93}
94
95impl error::Error for Error {
96 fn description(&self) -> &str {
97 "bitcoincore-rpc error"
98 }
99
100 fn cause(&self) -> Option<&dyn error::Error> {
101 match *self {
102 Error::JsonRpc(ref e) => Some(e),
103 Error::Hex(ref e) => Some(e),
104 Error::Json(ref e) => Some(e),
105 Error::BitcoinSerialization(ref e) => Some(e),
106 Error::Secp256k1(ref e) => Some(e),
107 Error::Io(ref e) => Some(e),
108 _ => None,
109 }
110 }
111}