1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! General error type used in the crate.
use std::{fmt, io};

#[cfg(any(
    feature = "rustls-webpki-roots",
    feature = "rustls-native-roots",
    feature = "rustls-platform-verifier"
))]
use rustls_pki_types::InvalidDnsNameError;
#[cfg(feature = "native-tls")]
use tokio_native_tls::native_tls;

use crate::proto::ProtocolError;

/// Generic error when using WebSockets with this crate.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Attempted to read from or write to a closed stream.
    AlreadyClosed,
    /// DNS lookup failed.
    CannotResolveHost,
    /// Attempted to connect a client to a remote without configured URI.
    #[cfg(feature = "client")]
    NoUriConfigured,
    /// WebSocket protocol violation.
    Protocol(ProtocolError),
    /// Payload length limit was exceeded.
    PayloadTooLong { len: usize, max_len: usize },
    /// I/O error.
    Io(io::Error),
    /// TLS error originating in [`native_tls`].
    #[cfg(feature = "native-tls")]
    NativeTls(native_tls::Error),
    /// Attempted to connect to an invalid DNS name.
    #[cfg(any(
        feature = "rustls-webpki-roots",
        feature = "rustls-native-roots",
        feature = "rustls-platform-verifier"
    ))]
    InvalidDNSName(InvalidDnsNameError),
    /// A general rustls error.
    #[cfg(any(
        feature = "rustls-webpki-roots",
        feature = "rustls-native-roots",
        feature = "rustls-platform-verifier"
    ))]
    Rustls(tokio_rustls::rustls::Error),
    /// An unsupported, i.e. not `ws` or `wss`, or no URI scheme was specified.
    #[cfg(feature = "client")]
    UnsupportedScheme,
    /// The HTTP/1.1 Upgrade failed.
    #[cfg(any(feature = "client", feature = "server"))]
    Upgrade(crate::upgrade::Error),
    /// Rustls was enabled via crate features, but no crypto provider was
    /// configured prior to connecting.
    #[cfg(all(
        any(
            feature = "rustls-webpki-roots",
            feature = "rustls-native-roots",
            feature = "rustls-platform-verifier"
        ),
        not(any(feature = "ring", feature = "aws_lc_rs"))
    ))]
    NoTlsConnectorConfigured,
}

#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
    fn from(err: native_tls::Error) -> Self {
        Self::NativeTls(err)
    }
}

impl From<ProtocolError> for Error {
    fn from(err: ProtocolError) -> Self {
        Self::Protocol(err)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Self::Io(err)
    }
}

#[cfg(any(
    feature = "rustls-webpki-roots",
    feature = "rustls-native-roots",
    feature = "rustls-platform-verifier"
))]
impl From<InvalidDnsNameError> for Error {
    fn from(err: InvalidDnsNameError) -> Self {
        Self::InvalidDNSName(err)
    }
}

#[cfg(any(
    feature = "rustls-webpki-roots",
    feature = "rustls-native-roots",
    feature = "rustls-platform-verifier"
))]
impl From<tokio_rustls::rustls::Error> for Error {
    fn from(err: tokio_rustls::rustls::Error) -> Self {
        Self::Rustls(err)
    }
}

#[cfg(any(feature = "client", feature = "server"))]
impl From<crate::upgrade::Error> for Error {
    fn from(err: crate::upgrade::Error) -> Self {
        Self::Upgrade(err)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::AlreadyClosed => {
                f.write_str("attempted to send message after closing connection")
            }
            Error::CannotResolveHost => f.write_str("client DNS lookup failed"),
            #[cfg(feature = "client")]
            Error::NoUriConfigured => f.write_str("client has no URI configured"),
            Error::Protocol(e) => e.fmt(f),
            Error::PayloadTooLong { len, max_len } => {
                f.write_str("payload length of ")?;
                len.fmt(f)?;
                f.write_str(" exceeds the limit of ")?;
                max_len.fmt(f)
            }
            Error::Io(e) => e.fmt(f),
            #[cfg(feature = "native-tls")]
            Error::NativeTls(e) => e.fmt(f),
            #[cfg(any(
                feature = "rustls-webpki-roots",
                feature = "rustls-native-roots",
                feature = "rustls-platform-verifier"
            ))]
            Error::InvalidDNSName(_) => f.write_str("invalid DNS name"),
            #[cfg(any(
                feature = "rustls-webpki-roots",
                feature = "rustls-native-roots",
                feature = "rustls-platform-verifier"
            ))]
            Error::Rustls(e) => e.fmt(f),
            #[cfg(feature = "client")]
            Error::UnsupportedScheme => f.write_str("unsupported or no URI scheme used"),
            #[cfg(any(feature = "client", feature = "server"))]
            Error::Upgrade(e) => e.fmt(f),
            #[cfg(all(
                any(
                    feature = "rustls-webpki-roots",
                    feature = "rustls-native-roots",
                    feature = "rustls-platform-verifier"
                ),
                not(any(feature = "ring", feature = "aws_lc_rs"))
            ))]
            Error::NoTlsConnectorConfigured => {
                f.write_str("wss uri set but no tls connector was configured")
            }
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::AlreadyClosed | Error::CannotResolveHost | Error::PayloadTooLong { .. } => None,
            #[cfg(feature = "client")]
            Error::NoUriConfigured => None,
            #[cfg(all(
                any(
                    feature = "rustls-webpki-roots",
                    feature = "rustls-native-roots",
                    feature = "rustls-platform-verifier"
                ),
                not(any(feature = "ring", feature = "aws_lc_rs"))
            ))]
            Error::NoTlsConnectorConfigured => None,
            #[cfg(feature = "client")]
            Error::UnsupportedScheme => None,
            Error::Protocol(e) => Some(e),
            Error::Io(e) => Some(e),
            #[cfg(feature = "native-tls")]
            Error::NativeTls(e) => Some(e),
            #[cfg(any(
                feature = "rustls-webpki-roots",
                feature = "rustls-native-roots",
                feature = "rustls-platform-verifier"
            ))]
            Error::InvalidDNSName(e) => Some(e),
            #[cfg(any(
                feature = "rustls-webpki-roots",
                feature = "rustls-native-roots",
                feature = "rustls-platform-verifier"
            ))]
            Error::Rustls(e) => Some(e),
            #[cfg(any(feature = "client", feature = "server"))]
            Error::Upgrade(e) => Some(e),
        }
    }
}