libp2prs_mplex/
error.rs

1// Copyright 2020 Netwarps Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use crate::frame::FrameDecodeError;
22
23/// The various error cases a connection may encounter.
24#[non_exhaustive]
25#[derive(Debug)]
26pub enum ConnectionError {
27    /// An underlying I/O error occured.
28    Io(std::io::Error),
29    /// Decoding a Yamux message frame failed.
30    Decode(FrameDecodeError),
31    /// The whole range of stream IDs has been used up.
32    NoMoreStreamIds,
33    /// An operation fails because the connection is closed.
34    Closed,
35    /// Too many streams are open, so no further ones can be opened at this time.
36    TooManyStreams,
37}
38
39impl std::fmt::Display for ConnectionError {
40    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41        match self {
42            ConnectionError::Io(e) => write!(f, "i/o error: {}", e),
43            ConnectionError::Decode(e) => write!(f, "decode error: {}", e),
44            ConnectionError::NoMoreStreamIds => f.write_str("number of stream ids has been exhausted"),
45            ConnectionError::Closed => f.write_str("connection is closed"),
46            ConnectionError::TooManyStreams => f.write_str("maximum number of streams reached"),
47        }
48    }
49}
50
51impl std::error::Error for ConnectionError {
52    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53        match self {
54            ConnectionError::Io(e) => Some(e),
55            ConnectionError::Decode(e) => Some(e),
56            ConnectionError::NoMoreStreamIds | ConnectionError::Closed | ConnectionError::TooManyStreams => None,
57        }
58    }
59}
60
61impl From<std::io::Error> for ConnectionError {
62    fn from(e: std::io::Error) -> Self {
63        ConnectionError::Io(e)
64    }
65}
66
67impl From<FrameDecodeError> for ConnectionError {
68    fn from(e: FrameDecodeError) -> Self {
69        ConnectionError::Decode(e)
70    }
71}
72
73impl From<futures::channel::mpsc::SendError> for ConnectionError {
74    fn from(_: futures::channel::mpsc::SendError) -> Self {
75        ConnectionError::Closed
76    }
77}
78
79impl From<futures::channel::oneshot::Canceled> for ConnectionError {
80    fn from(_: futures::channel::oneshot::Canceled) -> Self {
81        ConnectionError::Closed
82    }
83}