use crate::peer_list::PeerState;
use std::{
fmt::{self, Display, Formatter},
result,
};
#[derive(PartialEq, Eq, Debug)]
pub enum Error {
MismatchedPayload,
UnknownPayload,
MissingVotes,
SignatureFailure,
UnknownPeer,
InvalidPeerState {
required: PeerState,
actual: PeerState,
},
InvalidSelfState {
required: PeerState,
actual: PeerState,
},
InvalidEvent,
UnknownSelfParent,
UnknownOtherParent,
DuplicateVote,
PrematureGossip,
InvalidMessage,
DuplicateMessage,
FailedDkg,
Logic,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Error::MismatchedPayload => write!(
f,
"The payload of the vote doesn't match the payload of targeted block."
),
Error::UnknownPayload => write!(
f,
"The payload hash doesn't correspond to any payload known to our node."
),
Error::MissingVotes => write!(f, "Block cannot be created with no votes"),
Error::SignatureFailure => write!(
f,
"The message or signature might be corrupted, or the signer is wrong."
),
Error::UnknownPeer => write!(f, "The peer_id is not known to our node's peer_list."),
Error::InvalidPeerState { required, actual } => write!(
f,
"The peer is in invalid state (required: {:?}, actual: {:?}).",
required, actual
),
Error::InvalidSelfState { required, actual } => write!(
f,
"Our node is in invalid state (required: {:?}, actual: {:?}).",
required, actual
),
Error::InvalidEvent => write!(f, "The given event is invalid or malformed."),
Error::UnknownSelfParent => {
write!(f, "The event's self-parent is unknown to this node.")
}
Error::UnknownOtherParent => {
write!(f, "The event's other-parent is unknown to this node.")
}
Error::DuplicateVote => write!(f, "Our node has already voted for this network event."),
Error::PrematureGossip => write!(
f,
"The peer did not know we could handle a message from it."
),
Error::InvalidMessage => write!(f, "This non-empty message is invalid."),
Error::DuplicateMessage => write!(f, "This message has already been handled."),
Error::FailedDkg => write!(f, "The requested DKG could not proceed."),
Error::Logic => write!(
f,
"This is a logic error and represents a flaw in the code."
),
}
}
}
pub type Result<T> = result::Result<T, Error>;