Skip to content

Commit 8fcf28a

Browse files
committed
display an error page on panic
1 parent 3dc1299 commit 8fcf28a

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

risuto-web/index.html

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
5-
<link data-trunk rel="scss" href="scss/style.scss" />
6-
<link data-trunk rel="copy-dir" href="vendor/bootstrap-icons-1.10.2/fonts" />
7-
<link data-trunk rel="copy-file" href="vendor/bootstrap-5.2.2.bundle.min.js" />
8-
</head>
9-
<body>
10-
<!-- TODO: use data-trunk script instead of copy-file then script -->
11-
<script src="/bootstrap-5.2.2.bundle.min.js"></script>
12-
</body>
3+
4+
<head>
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<link data-trunk rel="scss" href="scss/style.scss" />
7+
<link data-trunk rel="copy-dir" href="vendor/bootstrap-icons-1.10.2/fonts" />
8+
<link data-trunk rel="copy-file" href="vendor/bootstrap-5.2.2.bundle.min.js" />
9+
</head>
10+
11+
<body id="body">
12+
<!-- TODO: use data-trunk script instead of copy-file then script -->
13+
<script src="/bootstrap-5.2.2.bundle.min.js"></script>
14+
</body>
15+
1316
</html>

risuto-web/panic-page.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>Something unexpected occurred</h1>
2+
<p>
3+
Please report an issue to <a href="https://github.com/Ekleog/risuto/issues">the bug tracker</a> with
4+
information about what you were doing and anything that could be unusual about your usage, as well as the
5+
below message:
6+
</p>
7+
<pre id="panic-message"></pre>

risuto-web/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(panic_info_message)]
12
use futures::{channel::oneshot, executor::block_on, FutureExt};
23
use gloo_storage::{LocalStorage, Storage};
34
use risuto_api::*;
@@ -11,6 +12,35 @@ mod ui;
1112

1213
fn main() {
1314
tracing_wasm::set_as_global_default();
15+
yew::set_custom_panic_hook(Box::new(|info| {
16+
let mut message = match info.location() {
17+
None => format!("Panic occurred at unknown place:\n"),
18+
Some(l) => format!(
19+
"Panic occurred at file '{}' line '{}':\n",
20+
l.file(),
21+
l.line()
22+
),
23+
};
24+
// TODO: when replacing this with console_error_panic_hook::stringify,
25+
// we can stop depending on nightly
26+
if let Some(m) = info.message() {
27+
let _ = std::fmt::write(&mut message, *m);
28+
} else {
29+
message += "failed recovering a message from the panic";
30+
}
31+
let document = web_sys::window()
32+
.expect("no web_sys window")
33+
.document()
34+
.expect("no web_sys document");
35+
document
36+
.get_element_by_id("body")
37+
.expect("no #body element")
38+
.set_inner_html(include_str!("../panic-page.html"));
39+
document
40+
.get_element_by_id("panic-message")
41+
.expect("no #panic-message element")
42+
.set_inner_html(&message);
43+
}));
1444
yew::start_app::<App>();
1545
}
1646

0 commit comments

Comments
 (0)