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
use futures_channel::mpsc::TrySendError;
#[cfg(all(feature = "serde_json", not(feature = "simd-json")))]
use serde_json::Error as JsonError;
#[cfg(feature = "simd-json")]
use simd_json::Error as JsonError;
use async_tungstenite::tungstenite::{Error as TungsteniteError, Message as TungsteniteMessage};
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
result::Result as StdResult,
};
use twilight_http::Error as HttpError;
use super::processor::Error as ProcessorError;
pub type Result<T, E = Error> = StdResult<T, E>;
#[derive(Debug)]
pub enum Error {
Processor {
source: ProcessorError,
},
Connecting {
source: TungsteniteError,
},
GettingGatewayUrl {
source: HttpError,
},
IdTooLarge {
id: u64,
total: u64,
},
LargeThresholdInvalid {
value: u64,
},
PayloadSerialization {
source: JsonError,
},
SendingMessage {
source: TrySendError<TungsteniteMessage>,
},
Stopped,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Self::Processor { .. } => f.write_str("An issue occured creating a shard processor"),
Self::Connecting { .. } => f.write_str("An issue occurred connecting to the gateway"),
Self::GettingGatewayUrl { .. } => f.write_str("Getting the gateway URL failed"),
Self::IdTooLarge { id, total } => {
write!(f, "The shard ID {} is larger than the total, {}", id, total)
}
Self::LargeThresholdInvalid { value } => write!(
f,
"The large threshold given, {}, is not in the accepted range",
value
),
Self::PayloadSerialization { .. } => {
f.write_str("Deserializing or serializing a payload failed")
}
Self::SendingMessage { .. } => {
f.write_str("The message couldn't be sent because the receiver half dropped")
}
Self::Stopped { .. } => f.write_str("the shard hasn't been started yet"),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Processor { source } => Some(source),
Self::Connecting { source } => Some(source),
Self::GettingGatewayUrl { source } => Some(source),
Self::PayloadSerialization { source } => Some(source),
Self::SendingMessage { source } => Some(source),
Self::IdTooLarge { .. } | Self::LargeThresholdInvalid { .. } | Self::Stopped { .. } => {
None
}
}
}
}