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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#.
The dialog shows panic payload, information about location of the panic and
if you want, an option to report the panic to developer and external links.
You will most likely want to use [`register`] to register the panic handler.
"#]
#![cfg_attr(
feature = "error-reporting",
doc = r#"
## A simple example
<img alt="Screenshot of a dialog with crash information" height="400" src="https://raw.githubusercontent.com/FireFragment/rust_gui_panic_handler/v0.1.0/docs/screenshot.png">
```rust,no_run
use gui_panic_handler::AppInfo;
use gui_panic_handler::Link;
gui_panic_handler::register(AppInfo {
name: "Sample app",
additional_text: "We are sorry, the application has crashed. To help us fix the crash, please report it using the button below.",
links: vec![
Link {
label: "Browse known crash causes",
url: "https://example.com",
},
Link {
label: "Get help on our forum",
url: "https://example.com",
},
Link {
label: "Our website",
url: "https://example.com",
},
],
report_bug_url: Some(gui_panic_handler::GitHubBugReporter::new(
String::from("FireFragment"),
String::from("rust_gui_panic_handler"),
)),
});
println!("Reading env var...");
let env_var_value = std::env::var("SUPER_IMPORTANT_ENVIRONMENT_VARIABLE").unwrap();
println!("Read: {env_var_value}")
```
"#
)]
#[cfg(feature = "error-reporting")]
mod error_reporting;
#[cfg(feature = "error-reporting")]
pub use error_reporting::*;
use eframe::egui::{self, Color32, RichText, Vec2};
/// Information about the application used in the error dialog box
#[cfg_attr(
feature = "error-reporting",
doc = r#"If you don't want to have bug report button, you can use [`AppInfoNoBugReport`] instead"#
)]
#[derive(Clone, Debug)]
pub struct AppInfo<#[cfg(feature = "error-reporting")] F: ReportBugUrlMaker> {
/// Name of the application
pub name: &'static str,
pub additional_text: &'static str,
/// Links to be displayed in the error dialog box
pub links: Vec<Link>,
/// Used to generate a URL for bug reports.
/// If you are using GitHub, you can use the ready-made [`GitHubBugReporter`] reporter.
///
/// You can use simple closure like this:
/// ```
/// # let report =
/// Some(|payload: Option<String>, bug_report| {
/// format!(
/// "https://github.com/FireFragment/rust_gui_panic_handler/issues/new?title=Unhandled panic: {}&body={}",
/// gui_panic_handler::urlencoding::encode(&payload.unwrap_or_default()),
/// gui_panic_handler::urlencoding::encode(&format!("### Panic report\n{bug_report}"))
/// )
/// })
/// # ; gui_panic_handler::AppInfo {
/// # name: "Sample app",
/// # additional_text: "",
/// # links: Vec::new(),
/// # report_bug_url: report,
/// # };
///
/// ```
///
/// If you don't want to have bug report button, you can disable the `error-reporting` feature or use [`AppInfoNoBugReport`] instead and set this field to `None`
#[cfg(feature = "error-reporting")]
pub report_bug_url: Option<F>,
}
/// A link to be displayed in the error dialog box
#[derive(Clone, Debug)]
pub struct Link {
pub label: &'static str,
pub url: &'static str,
}
/// Puts all details about the crash to a single [string](String).
///
/// Currently used for the `Report crash` and `Copy details` buttons
pub fn details<#[cfg(feature = "error-reporting")] F: ReportBugUrlMaker>(
panic_payload_display: &Option<String>,
panic_formatted: &String,
#[cfg(feature = "error-reporting")] app_info: &AppInfo<F>,
#[cfg(not(feature = "error-reporting"))] app_info: &AppInfo,
) -> String {
format!(
"**Panic report from {}**
{}
Package name: `{}`
Version: `{}`
Panic info:
```
{panic_formatted}
```",
panic_payload_display
.as_ref()
.unwrap_or(&String::from("[PAYLOAD IS NOT A STRING]")),
app_info.name,
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
}
/// Displays the panic dialog using [egui]
pub fn show_gui_egui<#[cfg(feature = "error-reporting")] F: ReportBugUrlMaker>(
panic_payload_display: Option<String>,
panic_formatted: String,
#[cfg(feature = "error-reporting")] app_info: AppInfo<F>,
#[cfg(not(feature = "error-reporting"))] app_info: AppInfo,
) {
eframe::run_simple_native(
"Crash report",
eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_maximize_button(false)
.with_always_on_top()
.with_inner_size([512.0, 256.0]),
..Default::default()
},
move |ctx, _frame| {
ctx.style_mut(|style| {
style.spacing.item_spacing = Vec2::new(4.0, 0.0);
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.vertical(|ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
ui.set_width(ui.available_width());
ui.horizontal(|ui| {
ui.label(RichText::new("âš ").size(48.0).color(Color32::RED));
ui.add_space(16.0);
ui.vertical(|ui| {
ui.heading(format!("{} crashed", app_info.name));
ui.add_space(8.0);
ui.label(app_info.additional_text);
if let Some(panic_payload_display) = &panic_payload_display {
ui.horizontal_wrapped(|ui| {
ui.strong("Reason:");
ui.monospace(panic_payload_display);
});
};
ui.add_space(8.0);
ui.horizontal_wrapped(|ui| {
if ui.button("📋 Copy details").clicked() {
ui.output_mut(|out| {
out.copied_text = details(
&panic_payload_display,
&panic_formatted,
&app_info,
);
});
}
#[cfg(feature = "error-reporting")]
if let Some(bug_report_url_maker) = &app_info.report_bug_url {
if ui.button("💬 Report crash").clicked() {
ui.output_mut(|out| {
out.open_url = Some(egui::OpenUrl {
url: bug_report_url_maker.get_report_url(
panic_payload_display.clone(),
details(
&panic_payload_display,
&panic_formatted,
&app_info,
),
),
new_tab: true,
});
});
}
}
});
ui.add_space(8.0);
ui.horizontal_wrapped(|ui| {
let mut links = app_info.links.iter();
if let Some(link) = links.next() {
if ui.link(link.label).clicked() {
ctx.open_url(egui::OpenUrl {
url: link.url.to_owned(),
new_tab: true,
});
};
}
for link in links {
ui.separator();
if ui.link(link.label).clicked() {
ctx.open_url(egui::OpenUrl {
url: link.url.to_owned(),
new_tab: true,
});
}
}
});
ui.add_space(16.0);
ui.horizontal_wrapped(|ui| {
ui.strong("Package name:");
ui.monospace(env!("CARGO_PKG_NAME"));
});
ui.horizontal_wrapped(|ui| {
ui.strong("Version:");
ui.label(env!("CARGO_PKG_VERSION"));
});
ui.collapsing("Developer information", |ui| {
ui.monospace(&panic_formatted);
/*ui.strong("Panic payload");
if let Some(panic_payload_debug) = &panic_payload_debug {
ui.monospace(panic_payload_debug);
} else {
ui.label("Panic payload doesn't implement");
ui.monospace("Debug");
}*/
});
})
});
});
});
});
},
)
.unwrap();
}
/// Register the panic handler. Run at the beggining of your program.
pub fn register<#[cfg(feature = "error-reporting")] F: ReportBugUrlMaker>(
#[cfg(feature = "error-reporting")] app_info: AppInfo<F>,
#[cfg(not(feature = "error-reporting"))] app_info: AppInfo,
) {
std::panic::set_hook(Box::new(move |panic_info| {
let panic_formatted = format!("{:#?}", panic_info);
let panic_payload_display = if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
Some(s.to_string())
} else {
panic_info
.payload()
.downcast_ref::<String>()
.map(|s| s.to_owned())
};
/*let panic_payload_display = Box::new(panic_info.payload())
.downcast_ref::<Box<dyn std::fmt::Display>>()
.map(|payload| format!("{payload}"));
let panic_payload_debug = Box::new(panic_info.payload())
.downcast_ref::<Box<dyn std::fmt::Debug>>()
.map(|payload| format!("{payload:#?}"));*/
println!("The app panicked.");
println!("Panic info: {panic_formatted}");
if let Some(panic_payload_display) = &panic_payload_display {
println!("Panic payload: {panic_payload_display}");
} else {
println!("Panic payload doesn't implement `Display`")
}
/*if let Some(panic_payload_debug) = &panic_payload_debug {
println!("Payload debug info: {panic_payload_debug}");
} else {
println!("Panic payload doesn't implement `Debug`")
}*/
show_gui_egui(panic_payload_display, panic_formatted, app_info.clone());
}));
}