Skip to main content

wse_client/
error.rs

1use std::fmt;
2
3/// Why a call failed or the client stopped.
4#[derive(Clone, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Error {
7    /// The configuration cannot work: a URL that is not `ws://` or
8    /// `wss://`, a header that cannot be sent.
9    Config(String),
10    /// The server could not be reached or did not upgrade the connection.
11    Connect(String),
12    /// The server refused the credentials: at the upgrade (HTTP 401 or
13    /// 403), or by closing with 4401, 4403 or 1008.
14    Auth(String),
15    /// The server did not confirm the connection in time.
16    Timeout(String),
17    /// The end-to-end session could not be opened or went wrong.
18    Encryption(String),
19    /// A signed message cannot be sent: no signing key, or no server key
20    /// to sign for.
21    Signing(String),
22    /// The client gave up reconnecting.
23    GaveUp(String),
24    /// Not connected now: the client is reconnecting.
25    NotConnected,
26    /// The send queue is full.
27    Full,
28    /// The client was closed.
29    Closed,
30    /// The application's handler panicked: the client stopped.
31    Panicked,
32    /// A message could not be encoded.
33    Encode(String),
34    /// A payload did not decode into the type asked for.
35    Decode(String),
36}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Config(why) => write!(f, "invalid configuration: {why}"),
42            Self::Connect(why) => write!(f, "cannot connect: {why}"),
43            Self::Auth(why) => write!(f, "authentication refused: {why}"),
44            Self::Timeout(why) => write!(f, "timed out: {why}"),
45            Self::Encryption(why) => write!(f, "end-to-end encryption failed: {why}"),
46            Self::Signing(why) => write!(f, "cannot sign: {why}"),
47            Self::GaveUp(why) => write!(f, "gave up reconnecting: {why}"),
48            Self::NotConnected => f.write_str("not connected"),
49            Self::Full => f.write_str("the send queue is full"),
50            Self::Closed => f.write_str("the client is closed"),
51            Self::Panicked => f.write_str("the handler panicked"),
52            Self::Encode(why) => write!(f, "cannot encode the message: {why}"),
53            Self::Decode(why) => write!(f, "cannot decode the payload: {why}"),
54        }
55    }
56}
57
58impl std::error::Error for Error {}