use std::io::{self, ErrorKind};
use thiserror::Error;
use tokio::sync::mpsc;
use crate::{
connection_manager::PeerConnectionError,
connectivity::ConnectivityError,
message::OutboundMessage,
protocol::ProtocolError,
};
#[derive(Debug, Error)]
pub enum MessagingProtocolError {
#[error("Failed to send message")]
MessageSendFailed,
#[error("ProtocolError: {0}")]
ProtocolError(#[from] ProtocolError),
#[error("PeerConnectionError: {0}")]
PeerConnectionError(#[from] PeerConnectionError),
#[error("Failed to dial peer: {0}")]
PeerDialFailed(ConnectivityError),
#[error("Connectivity error: {0}")]
ConnectivityError(#[from] ConnectivityError),
#[error("IO Error: {0}")]
Io(io::Error),
#[error("Sender error: {0}")]
SenderError(#[from] mpsc::error::SendError<OutboundMessage>),
#[error("Connection closed")]
ConnectionClosed(io::Error),
}
impl From<io::Error> for MessagingProtocolError {
fn from(err: io::Error) -> Self {
match err.kind() {
ErrorKind::ConnectionRefused |
ErrorKind::ConnectionReset |
ErrorKind::ConnectionAborted |
ErrorKind::BrokenPipe |
ErrorKind::WriteZero |
ErrorKind::NotConnected |
ErrorKind::UnexpectedEof => MessagingProtocolError::ConnectionClosed(err),
_ => MessagingProtocolError::Io(err),
}
}
}