use std::io;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ProtocolError {
#[error("IO error: {0}")]
IoError(#[from] io::Error),
#[error("Invalid flag: {0}")]
InvalidFlag(String),
#[error("The ProtocolId was longer than {}", u8::MAX)]
ProtocolIdTooLong,
#[error("Protocol negotiation failed because the peer did not accept any of the given protocols: {protocols}")]
ProtocolOutboundNegotiationFailed { protocols: String },
#[error("Protocol negotiation failed because the peer did not offer any protocols supported by this node")]
ProtocolInboundNegotiationFailed,
#[error("Optimistic protocol negotiation failed because the peer did not offer a protocol supported by this node")]
ProtocolOptimisticNegotiationFailed,
#[error("Protocol negotiation terminated by peer")]
ProtocolNegotiationTerminatedByPeer,
#[error("Protocol was not registered")]
ProtocolNotRegistered,
#[error("Failed to send notification because notification sender disconnected")]
NotificationSenderDisconnected,
#[error("Expected to ready bytes, that are not in the buffer")]
ExpectedReadyBytes,
}
impl ProtocolError {
pub fn is_ban_offence(&self) -> bool {
match self {
ProtocolError::IoError(_) |
ProtocolError::ProtocolNegotiationTerminatedByPeer |
ProtocolError::ProtocolOutboundNegotiationFailed { .. } |
ProtocolError::ProtocolNotRegistered |
ProtocolError::ProtocolInboundNegotiationFailed |
ProtocolError::ProtocolOptimisticNegotiationFailed |
ProtocolError::ExpectedReadyBytes |
ProtocolError::NotificationSenderDisconnected => false,
ProtocolError::ProtocolIdTooLong | ProtocolError::InvalidFlag(_) => true,
}
}
}