use core::fmt;
#[cfg(feature = "bitreq")]
use std::io;
#[cfg(feature = "bitreq")]
use corepc_types::bitcoin::{consensus::encode::FromHexError, hex::HexToArrayError};
use jsonrpc::serde_json;
#[derive(Debug)]
pub enum Error {
JsonRpc(jsonrpc::Error),
Json(serde_json::Error),
#[cfg(feature = "bitreq")]
DecodeHex(FromHexError),
#[cfg(feature = "bitreq")]
Model(Box<dyn core::error::Error + Send + Sync + 'static>),
#[cfg(feature = "bitreq")]
InvalidCookieFile,
#[cfg(feature = "bitreq")]
InvalidUrl(String),
#[cfg(feature = "bitreq")]
HexToArray(HexToArrayError),
#[cfg(feature = "bitreq")]
Io(io::Error),
#[cfg(feature = "bitreq")]
TryFromInt(core::num::TryFromIntError),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::JsonRpc(e) => write!(f, "JSON-RPC error: {e}"),
Error::Json(e) => write!(f, "JSON error: {e}"),
#[cfg(feature = "bitreq")]
Error::DecodeHex(e) => write!(f, "hex deserialization error: {e}"),
#[cfg(feature = "bitreq")]
Error::Model(e) => write!(f, "model conversion error: {e}"),
#[cfg(feature = "bitreq")]
Error::InvalidCookieFile => write!(f, "invalid or missing cookie file"),
#[cfg(feature = "bitreq")]
Error::InvalidUrl(e) => write!(f, "invalid RPC URL: {e}"),
#[cfg(feature = "bitreq")]
Error::HexToArray(e) => write!(f, "hash parsing error: {e}"),
#[cfg(feature = "bitreq")]
Error::Io(e) => write!(f, "I/O error: {e}"),
#[cfg(feature = "bitreq")]
Error::TryFromInt(e) => write!(f, "integer conversion overflow: {e}"),
}
}
}
impl core::error::Error for Error {}
impl Error {
#[cfg(feature = "bitreq")]
pub(crate) fn model<E>(e: E) -> Self
where
E: core::error::Error + Send + Sync + 'static,
{
Self::Model(Box::new(e))
}
pub(crate) fn transport<E>(e: E) -> Self
where
E: core::error::Error + Send + Sync + 'static,
{
Self::JsonRpc(jsonrpc::Error::Transport(Box::new(e)))
}
}
impl From<jsonrpc::Error> for Error {
fn from(e: jsonrpc::Error) -> Self {
Error::JsonRpc(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}
#[cfg(feature = "bitreq")]
impl From<HexToArrayError> for Error {
fn from(e: HexToArrayError) -> Self {
Error::HexToArray(e)
}
}
#[cfg(feature = "bitreq")]
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
#[cfg(feature = "bitreq")]
impl From<core::num::TryFromIntError> for Error {
fn from(e: core::num::TryFromIntError) -> Self {
Error::TryFromInt(e)
}
}
#[cfg(feature = "bitreq")]
impl From<FromHexError> for Error {
fn from(e: FromHexError) -> Self {
Error::DecodeHex(e)
}
}
impl Error {
pub fn is_not_found_error(&self) -> bool {
if let Error::JsonRpc(jsonrpc::Error::Rpc(rpc_err)) = self {
rpc_err.code == -5
} else {
false
}
}
}