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
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic, clippy::restriction)]
#![allow(
clippy::blanket_clippy_restriction_lints,
clippy::single_char_lifetime_names,
clippy::missing_inline_in_public_items,
clippy::implicit_return,
clippy::pattern_type_mismatch
)]
use std::{error::Error, fmt::Write, fs::OpenOptions, io::Write as _, path::PathBuf};
use twilight_http::Client;
use twilight_model::id::{
marker::{ChannelMarker, WebhookMarker},
Id,
};
pub struct ErrorHandler {
channel: Option<Id<ChannelMarker>>,
webhook: Option<(Id<WebhookMarker>, String)>,
file: Option<PathBuf>,
}
pub const DEFAULT_ERROR_MESSAGE: &str = "An error occurred, check the `stderr` for more info";
impl ErrorHandler {
#[must_use]
pub const fn new() -> Self {
Self {
channel: None,
webhook: None,
file: None,
}
}
pub fn channel(&mut self, channel_id: Id<ChannelMarker>) -> &mut Self {
self.channel = Some(channel_id);
self
}
pub fn webhook(&mut self, webhook_id: Id<WebhookMarker>, token: String) -> &mut Self {
self.webhook = Some((webhook_id, token));
self
}
pub fn file(&mut self, path: PathBuf) -> &mut Self {
self.file = Some(path);
self
}
#[allow(clippy::unwrap_used, unused_must_use, clippy::print_stderr)]
pub async fn handle(&self, http: &Client, error: impl Error + Send) {
let mut error_message = format!("{error}");
self.maybe_create_message(http, &mut error_message).await;
self.maybe_execute_webhook(http, &mut error_message).await;
self.maybe_append_error(&mut error_message);
eprintln!("{error_message}");
}
#[allow(clippy::print_stderr)]
pub fn handle_sync(&self, error: impl Error) {
let mut error_message = format!("{error}");
self.maybe_append_error(&mut error_message);
eprintln!("{error_message}");
}
#[allow(unused_must_use, clippy::unwrap_used)]
async fn maybe_create_message(&self, http: &Client, error_message: &mut String) {
if let Some(channel_id) = self.channel {
if let Err(err) = http
.create_message(channel_id)
.content(error_message)
.unwrap_or_else(|_| {
{
http.create_message(channel_id)
.content(DEFAULT_ERROR_MESSAGE)
}
.unwrap()
})
.exec()
.await
{
write!(error_message, "\n\nFailed to create message: {err}");
}
}
}
#[allow(unused_must_use, clippy::unwrap_used)]
async fn maybe_execute_webhook(&self, http: &Client, error_message: &mut String) {
if let Some((webhook_id, token)) = &self.webhook {
if let Err(err) = http
.execute_webhook(*webhook_id, token)
.content(error_message)
.unwrap_or_else(|_| {
http.execute_webhook(*webhook_id, token)
.content(DEFAULT_ERROR_MESSAGE)
.unwrap()
})
.exec()
.await
{
write!(error_message, "\n\nFailed to execute webhook: {err}");
}
}
}
#[allow(unused_must_use)]
fn maybe_append_error(&self, error_message: &mut String) {
if let Some(path) = &self.file {
if let Err(err) = OpenOptions::new()
.append(true)
.create(true)
.open(path)
.and_then(|mut file| file.write_all(error_message.as_ref()))
{
write!(error_message, "\n\nFailed to append to file: {err}");
}
}
}
}