liquid_rpc/
error.rs

1
2use std::{error, fmt, io};
3
4use bitcoin;
5use elements;
6use bitcoin::hashes::hex;
7use bitcoin::secp256k1;
8use jsonrpc;
9use serde_json;
10
11/// The error type for errors produced in this library.
12#[derive(Debug)]
13pub enum Error {
14    JsonRpc(jsonrpc::error::Error),
15    Hex(hex::Error),
16    Json(serde_json::error::Error),
17    BitcoinSerialization(bitcoin::consensus::encode::Error),
18    ElementsSerialization(elements::encode::Error),
19    Secp256k1(secp256k1::Error),
20    Io(io::Error),
21    InvalidAmount(bitcoin::util::amount::ParseAmountError),
22    InvalidCookieFile,
23    /// The JSON result had an unexpected structure.
24    UnexpectedStructure,
25}
26
27impl From<jsonrpc::error::Error> for Error {
28    fn from(e: jsonrpc::error::Error) -> Error {
29        Error::JsonRpc(e)
30    }
31}
32
33impl From<hex::Error> for Error {
34    fn from(e: hex::Error) -> Error {
35        Error::Hex(e)
36    }
37}
38
39impl From<serde_json::error::Error> for Error {
40    fn from(e: serde_json::error::Error) -> Error {
41        Error::Json(e)
42    }
43}
44
45impl From<bitcoin::consensus::encode::Error> for Error {
46    fn from(e: bitcoin::consensus::encode::Error) -> Error {
47        Error::BitcoinSerialization(e)
48    }
49}
50
51impl From<elements::encode::Error> for Error {
52    fn from(e: elements::encode::Error) -> Error {
53        Error::ElementsSerialization(e)
54    }
55}
56
57impl From<secp256k1::Error> for Error {
58    fn from(e: secp256k1::Error) -> Error {
59        Error::Secp256k1(e)
60    }
61}
62
63impl From<io::Error> for Error {
64    fn from(e: io::Error) -> Error {
65        Error::Io(e)
66    }
67}
68
69impl From<bitcoin::util::amount::ParseAmountError> for Error {
70    fn from(e: bitcoin::util::amount::ParseAmountError) -> Error {
71        Error::InvalidAmount(e)
72    }
73}
74
75impl From<bitcoincore_rpc::Error> for Error {
76    fn from(e: bitcoincore_rpc::Error) -> Error {
77        match e {
78            bitcoincore_rpc::Error::JsonRpc(e) => Error::JsonRpc(e),
79            bitcoincore_rpc::Error::Hex(e) => Error::Hex(e),
80            bitcoincore_rpc::Error::Json(e) => Error::Json(e),
81            bitcoincore_rpc::Error::BitcoinSerialization(e) => Error::BitcoinSerialization(e),
82            bitcoincore_rpc::Error::Secp256k1(e) => Error::Secp256k1(e),
83            bitcoincore_rpc::Error::Io(e) => Error::Io(e),
84            bitcoincore_rpc::Error::InvalidAmount(e) => Error::InvalidAmount(e),
85            bitcoincore_rpc::Error::InvalidCookieFile => Error::InvalidCookieFile,
86            bitcoincore_rpc::Error::UnexpectedStructure => Error::UnexpectedStructure,
87        }
88    }
89}
90
91impl fmt::Display for Error {
92    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93        match *self {
94            Error::JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
95            Error::Hex(ref e) => write!(f, "hex decode error: {}", e),
96            Error::Json(ref e) => write!(f, "JSON error: {}", e),
97            Error::BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
98            Error::ElementsSerialization(ref e) => write!(f, "Elements serialization error: {}", e),
99            Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
100            Error::Io(ref e) => write!(f, "I/O error: {}", e),
101            Error::InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
102            Error::InvalidCookieFile => write!(f, "invalid cookie file"),
103            Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
104        }
105    }
106}
107
108impl error::Error for Error {
109    fn description(&self) -> &str {
110        "bitcoincore-rpc error"
111    }
112
113    fn cause(&self) -> Option<&error::Error> {
114        match *self {
115            Error::JsonRpc(ref e) => Some(e),
116            Error::Hex(ref e) => Some(e),
117            Error::Json(ref e) => Some(e),
118            Error::BitcoinSerialization(ref e) => Some(e),
119            Error::ElementsSerialization(ref e) => Some(e),
120            Error::Secp256k1(ref e) => Some(e),
121            Error::Io(ref e) => Some(e),
122            _ => None,
123        }
124    }
125}