Skip to content

Commit c06435d

Browse files
committed
Make a dialog show when the editor panics
Part of #357
1 parent 913b936 commit c06435d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

editor/src/misc/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub enum EditorError {
2020
#[error("A rollback was initiated but no transaction was in progress")]
2121
NoTransactionInProgress,
2222

23+
#[error("Graphite panicked:\n{0}")]
24+
Panic(String),
25+
2326
#[error("{0}")]
2427
Misc(String),
2528
}

frontend/wasm/src/document.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::atomic::AtomicBool;
2+
13
use crate::shims::Error;
24
use crate::wrappers::{translate_key, translate_tool, Color};
35
use editor::input::input_preprocessor::ModifierKeys;
@@ -9,14 +11,33 @@ use editor::LayerId;
911
use graphene::layers::BlendMode;
1012
use wasm_bindgen::prelude::*;
1113

14+
static EDITOR_HAS_CRASHED: AtomicBool = AtomicBool::new(false);
15+
1216
fn convert_error(err: editor::EditorError) -> JsValue {
1317
Error::new(&err.to_string()).into()
1418
}
1519

1620
fn dispatch<T: Into<Message>>(message: T) -> Result<(), JsValue> {
17-
let result = crate::EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
18-
if let Ok(messages) = result {
19-
crate::handle_responses(messages);
21+
if EDITOR_HAS_CRASHED.load(std::sync::atomic::Ordering::SeqCst) {
22+
return Ok(());
23+
}
24+
25+
let result = crate::EDITOR_STATE
26+
.with(|state| state.try_borrow_mut().ok().map(|mut state| state.handle_message(message.into())))
27+
.unwrap_or_else(|| {
28+
Err(EditorError::Panic(String::from(
29+
"An internal error occurred. Reload the site to continue.\nSee the browser's console for details and please report this by filing an issue on GitHub.",
30+
)))
31+
});
32+
match result {
33+
Ok(messages) => {
34+
crate::handle_responses(messages);
35+
}
36+
Err(error) => {
37+
EDITOR_HAS_CRASHED.store(true, std::sync::atomic::Ordering::SeqCst);
38+
39+
crate::handle_response(FrontendMessage::DisplayError { description: error.to_string() });
40+
}
2041
}
2142
Ok(())
2243
}

0 commit comments

Comments
 (0)