libp2p_swarm/connection/
error.rs1use crate::transport::TransportError;
22use crate::Multiaddr;
23use crate::{ConnectedPoint, PeerId};
24use std::{fmt, io};
25
26#[derive(Debug)]
28pub enum ConnectionError<THandlerErr> {
29 IO(io::Error),
32
33 KeepAliveTimeout,
35
36 #[deprecated(
38 note = "Will be removed together with `ConnectionHandlerEvent::Close`. See <https://github.com/libp2p/rust-libp2p/issues/3591> for details."
39 )]
40 Handler(THandlerErr),
41}
42
43impl<THandlerErr> fmt::Display for ConnectionError<THandlerErr>
44where
45 THandlerErr: fmt::Display,
46{
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 ConnectionError::IO(err) => write!(f, "Connection error: I/O error: {err}"),
50 ConnectionError::KeepAliveTimeout => {
51 write!(f, "Connection closed due to expired keep-alive timeout.")
52 }
53 #[allow(deprecated)]
54 ConnectionError::Handler(err) => write!(f, "Connection error: Handler error: {err}"),
55 }
56 }
57}
58
59impl<THandlerErr> std::error::Error for ConnectionError<THandlerErr>
60where
61 THandlerErr: std::error::Error + 'static,
62{
63 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64 match self {
65 ConnectionError::IO(err) => Some(err),
66 ConnectionError::KeepAliveTimeout => None,
67 #[allow(deprecated)]
68 ConnectionError::Handler(err) => Some(err),
69 }
70 }
71}
72
73impl<THandlerErr> From<io::Error> for ConnectionError<THandlerErr> {
74 fn from(error: io::Error) -> Self {
75 ConnectionError::IO(error)
76 }
77}
78
79pub(crate) type PendingOutboundConnectionError =
85 PendingConnectionError<Vec<(Multiaddr, TransportError<io::Error>)>>;
86
87pub(crate) type PendingInboundConnectionError = PendingConnectionError<TransportError<io::Error>>;
89
90#[derive(Debug)]
92pub enum PendingConnectionError<TTransErr> {
93 Transport(TTransErr),
95
96 Aborted,
98
99 WrongPeerId {
102 obtained: PeerId,
103 endpoint: ConnectedPoint,
104 },
105
106 LocalPeerId { endpoint: ConnectedPoint },
108}
109
110impl<T> PendingConnectionError<T> {
111 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> PendingConnectionError<U> {
112 match self {
113 PendingConnectionError::Transport(t) => PendingConnectionError::Transport(f(t)),
114 PendingConnectionError::Aborted => PendingConnectionError::Aborted,
115 PendingConnectionError::WrongPeerId { obtained, endpoint } => {
116 PendingConnectionError::WrongPeerId { obtained, endpoint }
117 }
118 PendingConnectionError::LocalPeerId { endpoint } => {
119 PendingConnectionError::LocalPeerId { endpoint }
120 }
121 }
122 }
123}
124
125impl<TTransErr> fmt::Display for PendingConnectionError<TTransErr>
126where
127 TTransErr: fmt::Display + fmt::Debug,
128{
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 match self {
131 PendingConnectionError::Aborted => write!(f, "Pending connection: Aborted."),
132 PendingConnectionError::Transport(err) => {
133 write!(
134 f,
135 "Pending connection: Transport error on connection: {err}"
136 )
137 }
138 PendingConnectionError::WrongPeerId { obtained, endpoint } => {
139 write!(
140 f,
141 "Pending connection: Unexpected peer ID {obtained} at {endpoint:?}."
142 )
143 }
144 PendingConnectionError::LocalPeerId { endpoint } => {
145 write!(f, "Pending connection: Local peer ID at {endpoint:?}.")
146 }
147 }
148 }
149}
150
151impl<TTransErr> std::error::Error for PendingConnectionError<TTransErr>
152where
153 TTransErr: std::error::Error + 'static,
154{
155 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
156 match self {
157 PendingConnectionError::Transport(_) => None,
158 PendingConnectionError::WrongPeerId { .. } => None,
159 PendingConnectionError::LocalPeerId { .. } => None,
160 PendingConnectionError::Aborted => None,
161 }
162 }
163}