use std::{error, fmt, io};
use crate::bitcoin;
use crate::bitcoin::hashes::hex;
use crate::bitcoin::secp256k1;
use jsonrpc;
use serde_json;
#[derive(Debug)]
pub enum Error {
JsonRpc(jsonrpc::error::Error),
Hex(hex::HexToBytesError),
Json(serde_json::error::Error),
BitcoinSerialization(bitcoin::consensus::encode::FromHexError),
Secp256k1(secp256k1::Error),
Io(io::Error),
InvalidAmount(bitcoin::amount::ParseAmountError),
InvalidCookieFile,
UnexpectedStructure,
ReturnedError(String),
}
impl From<jsonrpc::error::Error> for Error {
fn from(e: jsonrpc::error::Error) -> Error {
Error::JsonRpc(e)
}
}
impl From<hex::HexToBytesError> for Error {
fn from(e: hex::HexToBytesError) -> Error {
Error::Hex(e)
}
}
impl From<serde_json::error::Error> for Error {
fn from(e: serde_json::error::Error) -> Error {
Error::Json(e)
}
}
impl From<bitcoin::consensus::encode::FromHexError> for Error {
fn from(e: bitcoin::consensus::encode::FromHexError) -> Error {
Error::BitcoinSerialization(e)
}
}
impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error {
Error::Secp256k1(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::Io(e)
}
}
impl From<bitcoin::amount::ParseAmountError> for Error {
fn from(e: bitcoin::amount::ParseAmountError) -> Error {
Error::InvalidAmount(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::JsonRpc(ref e) => write!(f, "JSON-RPC error: {}", e),
Error::Hex(ref e) => write!(f, "hex decode error: {}", e),
Error::Json(ref e) => write!(f, "JSON error: {}", e),
Error::BitcoinSerialization(ref e) => write!(f, "Bitcoin serialization error: {}", e),
Error::Secp256k1(ref e) => write!(f, "secp256k1 error: {}", e),
Error::Io(ref e) => write!(f, "I/O error: {}", e),
Error::InvalidAmount(ref e) => write!(f, "invalid amount: {}", e),
Error::InvalidCookieFile => write!(f, "invalid cookie file"),
Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
Error::ReturnedError(ref s) => write!(f, "the daemon returned an error string: {}", s),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
"bitcoincore-rpc error"
}
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::JsonRpc(ref e) => Some(e),
Error::Hex(ref e) => Some(e),
Error::Json(ref e) => Some(e),
Error::BitcoinSerialization(ref e) => Some(e),
Error::Secp256k1(ref e) => Some(e),
Error::Io(ref e) => Some(e),
_ => None,
}
}
}