1use crate::frame::FrameDecodeError;
12
13#[non_exhaustive]
15#[derive(Debug)]
16pub enum ConnectionError {
17 Io(std::io::Error),
19 Decode(FrameDecodeError),
21 NoMoreStreamIds,
23 Closed,
25 TooManyStreams,
27 InvalidWindowUpdate,
30}
31
32impl std::fmt::Display for ConnectionError {
33 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
34 match self {
35 ConnectionError::Io(e) => write!(f, "i/o error: {e}"),
36 ConnectionError::Decode(e) => write!(f, "decode error: {e}"),
37 ConnectionError::NoMoreStreamIds => {
38 f.write_str("number of stream ids has been exhausted")
39 }
40 ConnectionError::Closed => f.write_str("connection is closed"),
41 ConnectionError::TooManyStreams => f.write_str("maximum number of streams reached"),
42 ConnectionError::InvalidWindowUpdate => {
43 f.write_str("invalid window update for the current flow control window")
44 }
45 }
46 }
47}
48
49impl std::error::Error for ConnectionError {
50 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
51 match self {
52 ConnectionError::Io(e) => Some(e),
53 ConnectionError::Decode(e) => Some(e),
54 ConnectionError::NoMoreStreamIds
55 | ConnectionError::Closed
56 | ConnectionError::TooManyStreams => None,
57 ConnectionError::InvalidWindowUpdate => None,
58 }
59 }
60}
61
62impl From<std::io::Error> for ConnectionError {
63 fn from(e: std::io::Error) -> Self {
64 ConnectionError::Io(e)
65 }
66}
67
68impl From<FrameDecodeError> for ConnectionError {
69 fn from(e: FrameDecodeError) -> Self {
70 ConnectionError::Decode(e)
71 }
72}
73
74impl From<futures::channel::mpsc::SendError> for ConnectionError {
75 fn from(_: futures::channel::mpsc::SendError) -> Self {
76 ConnectionError::Closed
77 }
78}
79
80impl From<futures::channel::oneshot::Canceled> for ConnectionError {
81 fn from(_: futures::channel::oneshot::Canceled) -> Self {
82 ConnectionError::Closed
83 }
84}