use std::error::Error;
use std::sync::mpsc::SendError;
use protobuf::error::ProtobufError;
use super::{PeerId, ProposalId, ProposalUpdate};
#[derive(Debug)]
pub enum ProposalManagerError {
Internal(Box<dyn Error + Send>),
NotReady,
UnknownProposal(ProposalId),
UpdateSendFailed(SendError<ProposalUpdate>),
}
impl Error for ProposalManagerError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ProposalManagerError::Internal(err) => Some(&**err),
ProposalManagerError::NotReady => None,
ProposalManagerError::UnknownProposal(_) => None,
ProposalManagerError::UpdateSendFailed(err) => Some(err),
}
}
}
impl std::fmt::Display for ProposalManagerError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let msg = match self {
ProposalManagerError::Internal(err) => err.to_string(),
ProposalManagerError::NotReady => "not ready to process requests".to_string(),
ProposalManagerError::UnknownProposal(id) => {
format!("unknown proposal was specified: {}", id)
}
ProposalManagerError::UpdateSendFailed(err) => err.to_string(),
};
write!(f, "proposal manager error occurred: {}", msg)
}
}
impl From<SendError<ProposalUpdate>> for ProposalManagerError {
fn from(err: SendError<ProposalUpdate>) -> Self {
ProposalManagerError::UpdateSendFailed(err)
}
}
#[derive(Debug)]
pub enum ConsensusSendError {
Internal(Box<dyn Error + Send>),
NotReady,
UnknownPeer(PeerId),
}
impl Error for ConsensusSendError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ConsensusSendError::Internal(err) => Some(&**err),
ConsensusSendError::NotReady => None,
ConsensusSendError::UnknownPeer(_) => None,
}
}
}
impl std::fmt::Display for ConsensusSendError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ConsensusSendError::Internal(err) => {
write!(f, "internal error while sending consensus message: {}", err)
}
ConsensusSendError::NotReady => write!(f, "not ready to send messages"),
ConsensusSendError::UnknownPeer(peer_id) => {
write!(f, "attempted to send message to unknown peer: {}", peer_id)
}
}
}
}
impl From<ProtobufError> for ConsensusSendError {
fn from(err: ProtobufError) -> Self {
ConsensusSendError::Internal(Box::new(err))
}
}
#[derive(Debug)]
pub struct ConsensusEngineError(pub Box<dyn Error + Send>);
impl Error for ConsensusEngineError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&*self.0)
}
}
impl std::fmt::Display for ConsensusEngineError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"consensus engine unable to send consensus message: {}",
self.0
)
}
}
impl From<ProtobufError> for ConsensusEngineError {
fn from(err: ProtobufError) -> Self {
ConsensusEngineError(Box::new(err))
}
}
impl From<ProposalManagerError> for ConsensusEngineError {
fn from(err: ProposalManagerError) -> Self {
ConsensusEngineError(Box::new(err))
}
}
impl From<ConsensusSendError> for ConsensusEngineError {
fn from(err: ConsensusSendError) -> Self {
ConsensusEngineError(Box::new(err))
}
}