libp2p_websocket/
error.rs

1// Copyright 2019 Parity Technologies (UK) 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 std::{error, fmt};
22
23use libp2p_core::Multiaddr;
24
25use crate::tls;
26
27/// Error in WebSockets.
28#[derive(Debug)]
29pub enum Error<E> {
30    /// Error in the transport layer underneath.
31    Transport(E),
32    /// A TLS related error.
33    Tls(tls::Error),
34    /// Websocket handshake error.
35    Handshake(Box<dyn error::Error + Send + Sync>),
36    /// The configured maximum of redirects have been made.
37    TooManyRedirects,
38    /// A multi-address is not supported.
39    InvalidMultiaddr(Multiaddr),
40    /// The location header URL was invalid.
41    InvalidRedirectLocation,
42    /// Websocket base framing error.
43    Base(Box<dyn error::Error + Send + Sync>),
44}
45
46impl<E: fmt::Display> fmt::Display for Error<E> {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Error::Transport(err) => write!(f, "{err}"),
50            Error::Tls(err) => write!(f, "{err}"),
51            Error::Handshake(err) => write!(f, "{err}"),
52            Error::InvalidMultiaddr(ma) => write!(f, "invalid multi-address: {ma}"),
53            Error::TooManyRedirects => f.write_str("too many redirects"),
54            Error::InvalidRedirectLocation => f.write_str("invalid redirect location"),
55            Error::Base(err) => write!(f, "{err}"),
56        }
57    }
58}
59
60impl<E: error::Error + 'static> error::Error for Error<E> {
61    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
62        match self {
63            Error::Transport(err) => Some(err),
64            Error::Tls(err) => Some(err),
65            Error::Handshake(err) => Some(&**err),
66            Error::Base(err) => Some(&**err),
67            Error::InvalidMultiaddr(_)
68            | Error::TooManyRedirects
69            | Error::InvalidRedirectLocation => None,
70        }
71    }
72}
73
74impl<E> From<tls::Error> for Error<E> {
75    fn from(e: tls::Error) -> Self {
76        Error::Tls(e)
77    }
78}