use crate::frame::FrameDecodeError;
#[non_exhaustive]
#[derive(Debug)]
pub enum ConnectionError {
Io(std::io::Error),
Decode(FrameDecodeError),
NoMoreStreamIds,
Closed,
TooManyStreams,
}
impl std::fmt::Display for ConnectionError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ConnectionError::Io(e) => write!(f, "i/o error: {}", e),
ConnectionError::Decode(e) => write!(f, "decode error: {}", e),
ConnectionError::NoMoreStreamIds => {
f.write_str("number of stream ids has been exhausted")
}
ConnectionError::Closed => f.write_str("connection is closed"),
ConnectionError::TooManyStreams => f.write_str("maximum number of streams reached"),
}
}
}
impl std::error::Error for ConnectionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ConnectionError::Io(e) => Some(e),
ConnectionError::Decode(e) => Some(e),
ConnectionError::NoMoreStreamIds
| ConnectionError::Closed
| ConnectionError::TooManyStreams => None,
}
}
}
impl From<std::io::Error> for ConnectionError {
fn from(e: std::io::Error) -> Self {
ConnectionError::Io(e)
}
}
impl From<FrameDecodeError> for ConnectionError {
fn from(e: FrameDecodeError) -> Self {
ConnectionError::Decode(e)
}
}
impl From<futures::channel::mpsc::SendError> for ConnectionError {
fn from(_: futures::channel::mpsc::SendError) -> Self {
ConnectionError::Closed
}
}
impl From<futures::channel::oneshot::Canceled> for ConnectionError {
fn from(_: futures::channel::oneshot::Canceled) -> Self {
ConnectionError::Closed
}
}