use std::fmt;
#[derive(Debug)]
pub struct ConsensusError {
inner: anyhow::Error,
kind: ErrorKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
WalRecovery,
Internal,
}
impl ConsensusError {
pub fn wal_recovery(error: anyhow::Error) -> Self {
Self {
inner: error,
kind: ErrorKind::WalRecovery,
}
}
pub(crate) fn malachite<Ctx>(error: malachite_consensus::Error<Ctx>) -> Self
where
Ctx: malachite_types::Context,
{
let anyhow_err: anyhow::Error = error.into();
Self {
inner: anyhow_err,
kind: ErrorKind::Internal,
}
}
pub fn is_recoverable(&self) -> bool {
matches!(self.kind, ErrorKind::WalRecovery)
}
}
impl fmt::Display for ConsensusError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
impl std::error::Error for ConsensusError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.inner.source()
}
}