use std::fmt;
use crate::PeerAddr;
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
#[cfg(all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
))]
Decode(bitcode::Error),
#[cfg(all(feature = "serde", not(feature = "bitcode")))]
Encode(postcard::Error),
#[cfg(all(feature = "serde", not(feature = "bitcode")))]
Decode(postcard::Error),
#[cfg(not(any(
all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
),
all(feature = "serde", not(feature = "bitcode")),
)))]
Encode(Box<dyn std::error::Error + Send + Sync + 'static>),
#[cfg(not(any(
all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
),
all(feature = "serde", not(feature = "bitcode")),
)))]
Decode(Box<dyn std::error::Error + Send + Sync + 'static>),
ChannelClosed,
EngineStopped {
reason: String,
},
MessageTooLarge {
got: usize,
max: usize,
},
FrameEmpty,
FrameTruncated {
frame: &'static str,
field: &'static str,
},
NackInvalidLength {
len: usize,
},
UnknownAddrTag {
tag: u8,
},
UnknownFrameType {
tag: u8,
},
#[cfg(feature = "security")]
Security(snow::Error),
#[cfg(feature = "security")]
KeyMismatch,
#[cfg(feature = "security")]
HandshakeTimeout {
peer: PeerAddr,
},
Throttled,
#[cfg(feature = "security")]
Unreachable {
key: [u8; 32],
},
PeerTimeout {
peer: PeerAddr,
},
NoSession {
peer: PeerAddr,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O: {e}"),
#[cfg(all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
))]
Self::Decode(e) => write!(f, "decode: {e}"),
#[cfg(all(feature = "serde", not(feature = "bitcode")))]
Self::Encode(e) => write!(f, "encode: {e}"),
#[cfg(all(feature = "serde", not(feature = "bitcode")))]
Self::Decode(e) => write!(f, "decode: {e}"),
#[cfg(not(any(
all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
),
all(feature = "serde", not(feature = "bitcode")),
)))]
Self::Encode(e) => write!(f, "encode: {e}"),
#[cfg(not(any(
all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
),
all(feature = "serde", not(feature = "bitcode")),
)))]
Self::Decode(e) => write!(f, "decode: {e}"),
Self::ChannelClosed => f.write_str("channel closed"),
Self::EngineStopped { reason } => write!(f, "engine stopped: {reason}"),
Self::MessageTooLarge { got, max } => {
write!(f, "message needs {got} fragments, max is {max}")
}
Self::FrameEmpty => f.write_str("frame parse: empty packet"),
Self::FrameTruncated { frame, field } => {
write!(f, "frame parse: {frame} missing `{field}`")
}
Self::NackInvalidLength { len } => {
write!(f, "frame parse: nack body {len} B is not a multiple of 8")
}
Self::UnknownAddrTag { tag } => {
write!(f, "frame parse: unknown relay addr tag 0x{tag:02x}")
}
Self::UnknownFrameType { tag } => {
write!(f, "frame parse: unknown frame type 0x{tag:02x}")
}
#[cfg(feature = "security")]
Self::Security(e) => write!(f, "security: {e}"),
#[cfg(feature = "security")]
Self::KeyMismatch => f.write_str("security: remote key does not match pinned key"),
#[cfg(feature = "security")]
Self::HandshakeTimeout { peer } => {
write!(f, "security: handshake with {peer} timed out")
}
Self::Throttled => f.write_str("packet dropped: rate limit exceeded"),
#[cfg(feature = "security")]
Self::Unreachable { key } => {
write!(f, "relay cannot reach peer ")?;
key[..4].iter().try_for_each(|b| write!(f, "{b:02x}"))?;
f.write_str("…")
}
Self::NoSession { peer } => write!(f, "no secure session with {peer}"),
Self::PeerTimeout { peer } => write!(f, "peer {peer} timed out"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
#[cfg(all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
))]
Self::Decode(e) => Some(e),
#[cfg(all(feature = "serde", not(feature = "bitcode")))]
Self::Encode(e) => Some(e),
#[cfg(all(feature = "serde", not(feature = "bitcode")))]
Self::Decode(e) => Some(e),
#[cfg(not(any(
all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
),
all(feature = "serde", not(feature = "bitcode")),
)))]
Self::Encode(e) => Some(e.as_ref()),
#[cfg(not(any(
all(
feature = "bitcode",
not(feature = "serde"),
not(feature = "rkyv"),
not(feature = "wincode")
),
all(feature = "serde", not(feature = "bitcode")),
)))]
Self::Decode(e) => Some(e.as_ref()),
#[cfg(feature = "security")]
Self::Security(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
#[cfg(feature = "security")]
impl From<snow::Error> for Error {
fn from(e: snow::Error) -> Self {
Self::Security(e)
}
}