libp2prs_websocket/
error.rs

1// Copyright 2019 Parity Technologies (UK) Ltd.
2// Copyright 2020 Netwarps Ltd.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22use crate::tls;
23use libp2prs_core::transport::TransportError;
24use libp2prs_core::Multiaddr;
25use std::{error, fmt};
26
27/// Error in WebSockets.
28#[allow(dead_code)]
29#[derive(Debug)]
30pub(crate) enum WsError {
31    /// Error in the transport layer underneath.
32    Transport(TransportError),
33    /// A TLS related error.
34    Tls(tls::Error),
35    /// Websocket handshake error.
36    Handshake(Box<dyn error::Error + Send + Sync>),
37    /// The configured maximum of redirects have been made.
38    TooManyRedirects,
39    /// A multi-address is not supported.
40    InvalidMultiaddr(Multiaddr),
41    /// The location header URL was invalid.
42    InvalidRedirectLocation,
43    /// Websocket base framing error.
44    Base(Box<dyn error::Error + Send + Sync>),
45}
46
47impl fmt::Display for WsError {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            WsError::Transport(err) => write!(f, "{}", err),
51            WsError::Tls(err) => write!(f, "{}", err),
52            WsError::Handshake(err) => write!(f, "{}", err),
53            WsError::InvalidMultiaddr(ma) => write!(f, "invalid multi-address: {}", ma),
54            WsError::TooManyRedirects => f.write_str("too many redirects"),
55            WsError::InvalidRedirectLocation => f.write_str("invalid redirect location"),
56            WsError::Base(err) => write!(f, "{}", err),
57        }
58    }
59}
60
61impl error::Error for WsError {
62    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
63        match self {
64            WsError::Transport(err) => Some(err),
65            WsError::Tls(err) => Some(err),
66            WsError::Handshake(err) => Some(&**err),
67            WsError::Base(err) => Some(&**err),
68            WsError::InvalidMultiaddr(_) | WsError::TooManyRedirects | WsError::InvalidRedirectLocation => None,
69        }
70    }
71}
72
73impl From<tls::Error> for WsError {
74    fn from(e: tls::Error) -> Self {
75        WsError::Tls(e)
76    }
77}