djs_gateway/error.rs
1//! Errors returned by shards.
2
3use djs_model::gateway::CloseCode;
4
5/// A boxed error source.
6pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
7
8/// An error returned by [`Shard::next_event`](crate::Shard::next_event).
9///
10/// Most errors are **not fatal**: the shard has already scheduled a reconnect (or skipped the
11/// offending payload) and the next call to `next_event` continues normally. Only
12/// [`is_fatal`](Self::is_fatal) errors end the shard; after one, `next_event` returns `None`.
13///
14/// discord.js counterpart: the `error` event of `WebSocketShard` (`@discordjs/ws`).
15#[derive(Debug, thiserror::Error)]
16#[error("{kind}")]
17pub struct ReceiveError {
18 kind: ReceiveErrorKind,
19 #[source]
20 source: Option<BoxError>,
21}
22
23impl ReceiveError {
24 pub(crate) fn new(kind: ReceiveErrorKind) -> Self {
25 Self { kind, source: None }
26 }
27
28 pub(crate) fn with_source(kind: ReceiveErrorKind, source: impl Into<BoxError>) -> Self {
29 Self {
30 kind,
31 source: Some(source.into()),
32 }
33 }
34
35 /// Returns what went wrong.
36 ///
37 /// discord.js counterpart: the `error` event of `WebSocketShard` (`@discordjs/ws`).
38 #[must_use]
39 pub const fn kind(&self) -> &ReceiveErrorKind {
40 &self.kind
41 }
42
43 /// Returns `true` if the shard cannot continue (for example because the token is invalid
44 /// or a privileged intent is not enabled). No further events will be produced.
45 ///
46 /// discord.js counterpart: none (`@discordjs/ws` throws on fatal close codes).
47 #[must_use]
48 pub const fn is_fatal(&self) -> bool {
49 matches!(self.kind, ReceiveErrorKind::FatallyClosed { .. })
50 }
51
52 /// Consumes the error, returning its kind and underlying source.
53 ///
54 /// discord.js counterpart: none (JavaScript errors are not taken apart).
55 #[must_use]
56 pub fn into_parts(
57 self,
58 ) -> (
59 ReceiveErrorKind,
60 Option<Box<dyn std::error::Error + Send + Sync>>,
61 ) {
62 (self.kind, self.source)
63 }
64}
65
66/// The kind of a [`ReceiveError`].
67///
68/// discord.js counterpart: the `error` event of `WebSocketShard` (`@discordjs/ws`).
69#[derive(Debug, thiserror::Error)]
70#[non_exhaustive]
71pub enum ReceiveErrorKind {
72 /// Connecting to the gateway failed. The shard retries with exponential backoff.
73 #[error("failed to connect to the gateway")]
74 Connect,
75 /// Reading from or writing to the WebSocket failed. The shard reconnects and resumes.
76 #[error("the gateway connection failed")]
77 Connection,
78 /// A compressed message could not be decompressed. The shard reconnects (a broken
79 /// decompression context cannot be recovered).
80 #[error("failed to decompress a gateway message")]
81 Decompress,
82 /// A payload could not be deserialized. It was skipped; the connection is unaffected.
83 #[error("failed to deserialize a gateway payload")]
84 Deserialize,
85 /// Discord closed the connection with a close code that forbids reconnecting.
86 #[error("the gateway closed the connection with {code:?}; reconnecting is not allowed")]
87 FatallyClosed {
88 /// The close code Discord sent.
89 code: CloseCode,
90 },
91}
92
93/// An error returned by [`MessageSender`](crate::MessageSender).
94///
95/// discord.js counterpart: none (`WebSocketShard#send` rejects with a plain `Error`).
96#[derive(Debug, thiserror::Error)]
97#[non_exhaustive]
98pub enum SendError {
99 /// The command could not be serialized.
100 #[error("failed to serialize the gateway command")]
101 Serialize(#[source] serde_json::Error),
102 /// The shard has been dropped, so the command can never be sent.
103 #[error("the shard is gone")]
104 Closed,
105}