bitcoincore_rpc_async/
error.rs1use 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#[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 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
40
41impl From<hex::Error> for Error {
42 fn from(e: hex::Error) -> Error {
43 Error::Hex(e)
44 }
45}
46
47impl From<serde_json::error::Error> for Error {
48 fn from(e: serde_json::error::Error) -> Error {
49 Error::Json(e)
50 }
51}
52
53impl From<bitcoin::consensus::encode::Error> for Error {
54 fn from(e: bitcoin::consensus::encode::Error) -> Error {
55 Error::BitcoinSerialization(e)
56 }
57}
58
59impl From<secp256k1::Error> for Error {
60 fn from(e: secp256k1::Error) -> Error {
61 Error::Secp256k1(e)
62 }
63}
64
65impl From<io::Error> for Error {
66 fn from(e: io::Error) -> Error {
67 Error::Io(e)
68 }
69}
70
71impl From<bitcoin::util::amount::ParseAmountError> for Error {
72 fn from(e: bitcoin::util::amount::ParseAmountError) -> Error {
73 Error::InvalidAmount(e)
74 }
75}
76
77impl fmt::Display for Error {
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79 match *self {
80 Error::JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
81 Error::Hex(ref e) => write!(f, "hex decode error: {}", e),
82 Error::Json(ref e) => write!(f, "JSON error: {}", e),
83 Error::BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
84 Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
85 Error::Io(ref e) => write!(f, "I/O error: {}", e),
86 Error::InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
87 Error::InvalidCookieFile => write!(f, "invalid cookie file"),
88 Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
89 }
90 }
91}
92
93impl error::Error for Error {
94 fn description(&self) -> &str {
95 "bitcoincore-rpc error"
96 }
97
98 fn cause(&self) -> Option<&dyn error::Error> {
99 match *self {
100 Error::JsonRpc(ref e) => Some(e),
101 Error::Hex(ref e) => Some(e),
102 Error::Json(ref e) => Some(e),
103 Error::BitcoinSerialization(ref e) => Some(e),
104 Error::Secp256k1(ref e) => Some(e),
105 Error::Io(ref e) => Some(e),
106 _ => None,
107 }
108 }
109}