|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::transport::TransportError; |
|
|
use crate::Multiaddr; |
|
|
use crate::{ConnectedPoint, PeerId}; |
|
|
use std::{fmt, io}; |
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
pub enum ConnectionError { |
|
|
|
|
|
|
|
|
IO(io::Error), |
|
|
|
|
|
|
|
|
KeepAliveTimeout, |
|
|
} |
|
|
|
|
|
impl fmt::Display for ConnectionError { |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
match self { |
|
|
ConnectionError::IO(err) => write!(f, "Connection error: I/O error: {err}"), |
|
|
ConnectionError::KeepAliveTimeout => { |
|
|
write!(f, "Connection closed due to expired keep-alive timeout.") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl std::error::Error for ConnectionError { |
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
|
|
match self { |
|
|
ConnectionError::IO(err) => Some(err), |
|
|
ConnectionError::KeepAliveTimeout => None, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl From<io::Error> for ConnectionError { |
|
|
fn from(error: io::Error) -> Self { |
|
|
ConnectionError::IO(error) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) type PendingOutboundConnectionError = |
|
|
PendingConnectionError<Vec<(Multiaddr, TransportError<io::Error>)>>; |
|
|
|
|
|
|
|
|
pub(crate) type PendingInboundConnectionError = PendingConnectionError<TransportError<io::Error>>; |
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
pub enum PendingConnectionError<TTransErr> { |
|
|
|
|
|
Transport(TTransErr), |
|
|
|
|
|
|
|
|
Aborted, |
|
|
|
|
|
|
|
|
|
|
|
WrongPeerId { |
|
|
obtained: PeerId, |
|
|
endpoint: ConnectedPoint, |
|
|
}, |
|
|
|
|
|
|
|
|
LocalPeerId { endpoint: ConnectedPoint }, |
|
|
} |
|
|
|
|
|
impl<T> PendingConnectionError<T> { |
|
|
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> PendingConnectionError<U> { |
|
|
match self { |
|
|
PendingConnectionError::Transport(t) => PendingConnectionError::Transport(f(t)), |
|
|
PendingConnectionError::Aborted => PendingConnectionError::Aborted, |
|
|
PendingConnectionError::WrongPeerId { obtained, endpoint } => { |
|
|
PendingConnectionError::WrongPeerId { obtained, endpoint } |
|
|
} |
|
|
PendingConnectionError::LocalPeerId { endpoint } => { |
|
|
PendingConnectionError::LocalPeerId { endpoint } |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<TTransErr> fmt::Display for PendingConnectionError<TTransErr> |
|
|
where |
|
|
TTransErr: fmt::Display + fmt::Debug, |
|
|
{ |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
match self { |
|
|
PendingConnectionError::Aborted => write!(f, "Pending connection: Aborted."), |
|
|
PendingConnectionError::Transport(err) => { |
|
|
write!( |
|
|
f, |
|
|
"Pending connection: Transport error on connection: {err}" |
|
|
) |
|
|
} |
|
|
PendingConnectionError::WrongPeerId { obtained, endpoint } => { |
|
|
write!( |
|
|
f, |
|
|
"Pending connection: Unexpected peer ID {obtained} at {endpoint:?}." |
|
|
) |
|
|
} |
|
|
PendingConnectionError::LocalPeerId { endpoint } => { |
|
|
write!(f, "Pending connection: Local peer ID at {endpoint:?}.") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<TTransErr> std::error::Error for PendingConnectionError<TTransErr> |
|
|
where |
|
|
TTransErr: std::error::Error + 'static, |
|
|
{ |
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
|
|
match self { |
|
|
PendingConnectionError::Transport(_) => None, |
|
|
PendingConnectionError::WrongPeerId { .. } => None, |
|
|
PendingConnectionError::LocalPeerId { .. } => None, |
|
|
PendingConnectionError::Aborted => None, |
|
|
} |
|
|
} |
|
|
} |
|
|
|