serenity/gateway/error.rs
1use std::error::Error as StdError;
2use std::fmt;
3
4use tokio_tungstenite::tungstenite::protocol::CloseFrame;
5
6/// An error that occurred while attempting to deal with the gateway.
7///
8/// Note that - from a user standpoint - there should be no situation in which you manually handle
9/// these.
10#[derive(Clone, Debug)]
11#[non_exhaustive]
12pub enum Error {
13 /// There was an error building a URL.
14 BuildingUrl,
15 /// The connection closed, potentially uncleanly.
16 Closed(Option<CloseFrame<'static>>),
17 /// Expected a Hello during a handshake
18 ExpectedHello,
19 /// When there was an error sending a heartbeat.
20 HeartbeatFailed,
21 /// When invalid authentication (a bad token) was sent in the IDENTIFY.
22 InvalidAuthentication,
23 /// Expected a Ready or an InvalidateSession
24 InvalidHandshake,
25 /// When invalid sharding data was sent in the IDENTIFY.
26 ///
27 /// # Examples
28 ///
29 /// Sending a shard ID of 5 when sharding with 3 total is considered invalid.
30 InvalidShardData,
31 /// When no authentication was sent in the IDENTIFY.
32 NoAuthentication,
33 /// When a session Id was expected (for resuming), but was not present.
34 NoSessionId,
35 /// When a shard would have too many guilds assigned to it.
36 ///
37 /// # Examples
38 ///
39 /// When sharding 5500 guilds on 2 shards, at least one of the shards will have over the
40 /// maximum number of allowed guilds per shard.
41 ///
42 /// This limit is currently 2500 guilds per shard.
43 OverloadedShard,
44 /// Failed to reconnect after a number of attempts.
45 ReconnectFailure,
46 /// When undocumented gateway intents are provided.
47 InvalidGatewayIntents,
48 /// When disallowed gateway intents are provided.
49 ///
50 /// If an connection has been established but privileged gateway intents were provided without
51 /// enabling them prior.
52 DisallowedGatewayIntents,
53}
54
55impl fmt::Display for Error {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 Self::BuildingUrl => f.write_str("Error building url"),
59 Self::Closed(_) => f.write_str("Connection closed"),
60 Self::ExpectedHello => f.write_str("Expected a Hello"),
61 Self::HeartbeatFailed => f.write_str("Failed sending a heartbeat"),
62 Self::InvalidAuthentication => f.write_str("Sent invalid authentication"),
63 Self::InvalidHandshake => f.write_str("Expected a valid Handshake"),
64 Self::InvalidShardData => f.write_str("Sent invalid shard data"),
65 Self::NoAuthentication => f.write_str("Sent no authentication"),
66 Self::NoSessionId => f.write_str("No Session Id present when required"),
67 Self::OverloadedShard => f.write_str("Shard has too many guilds"),
68 Self::ReconnectFailure => f.write_str("Failed to Reconnect"),
69 Self::InvalidGatewayIntents => f.write_str("Invalid gateway intents were provided"),
70 Self::DisallowedGatewayIntents => {
71 f.write_str("Disallowed gateway intents were provided")
72 },
73 }
74 }
75}
76
77impl StdError for Error {}