Skip to content

Commit

Permalink
display an error page on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog committed Dec 4, 2022
1 parent 3dc1299 commit 8fcf28a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
23 changes: 13 additions & 10 deletions risuto-web/index.html
@@ -1,13 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link data-trunk rel="scss" href="scss/style.scss" />
<link data-trunk rel="copy-dir" href="vendor/bootstrap-icons-1.10.2/fonts" />
<link data-trunk rel="copy-file" href="vendor/bootstrap-5.2.2.bundle.min.js" />
</head>
<body>
<!-- TODO: use data-trunk script instead of copy-file then script -->
<script src="/bootstrap-5.2.2.bundle.min.js"></script>
</body>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link data-trunk rel="scss" href="scss/style.scss" />
<link data-trunk rel="copy-dir" href="vendor/bootstrap-icons-1.10.2/fonts" />
<link data-trunk rel="copy-file" href="vendor/bootstrap-5.2.2.bundle.min.js" />
</head>

<body id="body">
<!-- TODO: use data-trunk script instead of copy-file then script -->
<script src="/bootstrap-5.2.2.bundle.min.js"></script>
</body>

</html>
7 changes: 7 additions & 0 deletions risuto-web/panic-page.html
@@ -0,0 +1,7 @@
<h1>Something unexpected occurred</h1>
<p>
Please report an issue to <a href="https://github.com/Ekleog/risuto/issues">the bug tracker</a> with
information about what you were doing and anything that could be unusual about your usage, as well as the
below message:
</p>
<pre id="panic-message"></pre>
30 changes: 30 additions & 0 deletions risuto-web/src/main.rs
@@ -1,3 +1,4 @@
#![feature(panic_info_message)]
use futures::{channel::oneshot, executor::block_on, FutureExt};
use gloo_storage::{LocalStorage, Storage};
use risuto_api::*;
Expand All @@ -11,6 +12,35 @@ mod ui;

fn main() {
tracing_wasm::set_as_global_default();
yew::set_custom_panic_hook(Box::new(|info| {
let mut message = match info.location() {
None => format!("Panic occurred at unknown place:\n"),
Some(l) => format!(
"Panic occurred at file '{}' line '{}':\n",
l.file(),
l.line()
),
};
// TODO: when replacing this with console_error_panic_hook::stringify,
// we can stop depending on nightly
if let Some(m) = info.message() {
let _ = std::fmt::write(&mut message, *m);
} else {
message += "failed recovering a message from the panic";
}
let document = web_sys::window()
.expect("no web_sys window")
.document()
.expect("no web_sys document");
document
.get_element_by_id("body")
.expect("no #body element")
.set_inner_html(include_str!("../panic-page.html"));
document
.get_element_by_id("panic-message")
.expect("no #panic-message element")
.set_inner_html(&message);
}));
yew::start_app::<App>();
}

Expand Down

0 comments on commit 8fcf28a

Please sign in to comment.