Show a crash dialog when the editor panics (#362)

* Show a crash dialog when the editor panics

Closes #357

* Suppress console usage lints

* Proxy cleanup and comments
This commit is contained in:
Keavon Chambers 2021-08-31 18:34:19 -07:00
parent c8389bbae1
commit d290aaf712
16 changed files with 212 additions and 32 deletions

View file

@ -5,6 +5,7 @@ pub mod wrappers;
use editor::{message_prelude::*, Editor};
use std::cell::RefCell;
use std::sync::atomic::AtomicBool;
use utils::WasmLog;
use wasm_bindgen::prelude::*;
@ -13,6 +14,7 @@ thread_local! {
pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new());
}
static LOGGER: WasmLog = WasmLog;
static EDITOR_HAS_CRASHED: AtomicBool = AtomicBool::new(false);
#[wasm_bindgen(start)]
pub fn init() {
@ -22,21 +24,41 @@ pub fn init() {
}
// Sends FrontendMessages to JavaScript
pub fn dispatch<T: Into<Message>>(message: T) {
let messages = EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
for message in messages.into_iter() {
let message_type = message.to_discriminant().local_name();
let message_data = JsValue::from_serde(&message).expect("Failed to serialize response");
let _ = handleResponse(message_type, message_data).map_err(|error| {
log::error!(
"While handling FrontendMessage \"{:?}\", JavaScript threw an error: {:?}",
message.to_discriminant().local_name(),
error
)
});
fn dispatch<T: Into<Message>>(message: T) {
// Process no further messages after a crash to avoid spamming the console
if EDITOR_HAS_CRASHED.load(std::sync::atomic::Ordering::SeqCst) {
return;
}
match EDITOR_STATE.with(|state| state.try_borrow_mut().ok().map(|mut state| state.handle_message(message.into()))) {
Some(messages) => {
for message in messages.into_iter() {
handle_response(message);
}
}
None => {
EDITOR_HAS_CRASHED.store(true, std::sync::atomic::Ordering::SeqCst);
let title = "The editor crashed — sorry about that".to_string();
let description = "An internal error occurred. Reload the editor to continue. Please report this by filing an issue on GitHub.".to_string();
handle_response(FrontendMessage::DisplayPanic { title, description });
}
}
}
// Sends a FrontendMessage to JavaScript
fn handle_response(message: FrontendMessage) {
let message_type = message.to_discriminant().local_name();
let message_data = JsValue::from_serde(&message).expect("Failed to serialize response");
let _ = handleResponse(message_type, message_data).map_err(|error| {
log::error!(
"While handling FrontendMessage \"{:?}\", JavaScript threw an error: {:?}",
message.to_discriminant().local_name(),
error
)
});
}
// The JavaScript function to call into