use crate::{
methods::{Method, MethodError},
utils::HexU32Be,
};
#[derive(Debug)]
#[non_exhaustive]
pub enum Error<'a> {
BadBytesConvert(binary_sv2::Error),
BTCHashError(bitcoin_hashes::Error),
HexError(bitcoin_hashes::Error),
IncorrectClientStatus(String),
Infallible(std::convert::Infallible),
InvalidJsonRpcMessageKind,
InvalidHexLen(String),
#[allow(clippy::upper_case_acronyms)]
InvalidReceiver(Box<Method<'a>>),
InvalidSubmission,
Method(Box<MethodError<'a>>),
UnauthorizedClient(String),
UnknownID(u64),
InvalidVersionMask(HexU32Be),
UnexpectedMessage(String),
}
impl std::fmt::Display for Error<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::BadBytesConvert(ref e) => write!(
f,
"Bad U256 or B032 conversion (U256 length must be exactly 32 bytes; B032 length must be <= 32 bytes): {e:?}"
),
Error::BTCHashError(ref e) => write!(f, "Bitcoin Hashes Error: `{e:?}`"),
Error::HexError(ref e) => write!(f, "Bad hex encode/decode: `{e:?}`"),
Error::InvalidHexLen(ref val) => write!(f, "Hex string length is invalid: `{val}`"),
Error::IncorrectClientStatus(s) => {
write!(f, "Client status is incompatible with message: `{s}`")
}
Error::Infallible(ref e) => write!(f, "Infallible error{e:?}"),
Error::InvalidJsonRpcMessageKind => write!(
f,
"Server received a `json_rpc` response when it should only receive requests"
),
Error::InvalidReceiver(ref e) => write!(
f,
"Client received an invalid message that was intended to be sent from the
client to the server, NOT from the server to the client. Invalid message: `{e:?}`"
),
Error::InvalidSubmission => {
write!(f, "Server received an invalid `mining.submit` message.")
}
Error::Method(ref e) => {
write!(
f,
"Error converting valid `json_rpc` SV1 message: `{e:?}`"
)
}
Error::UnauthorizedClient(id) => write!(
f,
"Client with id `{id}` expected to be authorized but is unauthorized."
),
Error::UnknownID(e) => write!(f, "Server did not recognize the client id: `{e}`."),
Error::InvalidVersionMask(e) => write!(f, "First 3 bits of version rolling mask must be 0 and last 13 bits of version rolling mask must be 0. Version rolling mask is: `{:b}`.", e.0),
Error::UnexpectedMessage(method) => write!(f, "Unexpected or unsupported message/method called: `{method}`."),
}
}
}
impl From<bitcoin_hashes::Error> for Error<'_> {
fn from(e: bitcoin_hashes::Error) -> Self {
Error::BTCHashError(e)
}
}
impl From<std::convert::Infallible> for Error<'_> {
fn from(e: std::convert::Infallible) -> Self {
Error::Infallible(e)
}
}
impl<'a> From<MethodError<'a>> for Error<'a> {
fn from(inner: MethodError<'a>) -> Self {
Error::Method(Box::new(inner))
}
}
impl From<binary_sv2::Error> for Error<'_> {
fn from(inner: binary_sv2::Error) -> Self {
Error::BadBytesConvert(inner)
}
}