Skip to content

Commit 51c2494

Browse files
authored
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
1 parent 1999905 commit 51c2494

File tree

16 files changed

+212
-32
lines changed

16 files changed

+212
-32
lines changed

editor/src/document/document_message_handler.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ impl MessageHandler<DocumentsMessage, &InputPreprocessor> for DocumentsMessageHa
202202
Ok(document) => {
203203
self.load_document(document, responses);
204204
}
205-
Err(e) => responses.push_back(FrontendMessage::DisplayError { description: e.to_string() }.into()),
205+
Err(e) => responses.push_back(
206+
FrontendMessage::DisplayError {
207+
title: "Failed to open document".to_string(),
208+
description: e.to_string(),
209+
}
210+
.into(),
211+
),
206212
}
207213
}
208214
GetOpenDocumentsList => {

editor/src/frontend/frontend_message_handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub enum FrontendMessage {
1212
SetActiveTool { tool_name: String, tool_options: Option<ToolOptions> },
1313
SetActiveDocument { document_index: usize },
1414
UpdateOpenDocumentsList { open_documents: Vec<String> },
15-
DisplayError { description: String },
15+
DisplayError { title: String, description: String },
16+
DisplayPanic { title: String, description: String },
1617
DisplayConfirmationToCloseDocument { document_index: usize },
1718
DisplayConfirmationToCloseAllDocuments,
1819
UpdateCanvas { document: String },

editor/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ impl Editor {
4141

4242
pub fn handle_message<T: Into<Message>>(&mut self, message: T) -> Vec<FrontendMessage> {
4343
self.dispatcher.handle_message(message);
44+
4445
let mut responses = Vec::new();
4546
std::mem::swap(&mut responses, &mut self.dispatcher.responses);
47+
4648
responses
4749
}
4850
}

frontend/src/components/panels/Document.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ import { defineComponent } from "vue";
227227
import { ResponseType, registerResponseHandler, Response, UpdateCanvas, UpdateScrollbars, SetActiveTool, SetCanvasZoom, SetCanvasRotation } from "@/utilities/response-handler";
228228
import { SeparatorDirection, SeparatorType } from "@/components/widgets/widgets";
229229
import { comingSoon } from "@/utilities/errors";
230+
import { panicProxy } from "@/utilities/panic";
230231
231232
import LayoutRow from "@/components/layout/LayoutRow.vue";
232233
import LayoutCol from "@/components/layout/LayoutCol.vue";
@@ -245,7 +246,7 @@ import OptionalInput from "@/components/widgets/inputs/OptionalInput.vue";
245246
import ToolOptions from "@/components/widgets/options/ToolOptions.vue";
246247
import { SectionsOfMenuListEntries } from "@/components/widgets/floating-menus/MenuList.vue";
247248
248-
const wasm = import("@/../wasm/pkg");
249+
const wasm = import("@/../wasm/pkg").then(panicProxy);
249250
250251
const documentModeEntries: SectionsOfMenuListEntries = [
251252
[

frontend/src/components/panels/LayerTree.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
import { defineComponent } from "vue";
183183
184184
import { ResponseType, registerResponseHandler, Response, BlendMode, ExpandFolder, CollapseFolder, UpdateLayer, LayerPanelEntry, LayerType } from "@/utilities/response-handler";
185+
import { panicProxy } from "@/utilities/panic";
185186
import { SeparatorType } from "@/components/widgets/widgets";
186187
187188
import LayoutRow from "@/components/layout/LayoutRow.vue";
@@ -195,7 +196,7 @@ import IconLabel from "@/components/widgets/labels/IconLabel.vue";
195196
import DropdownInput from "@/components/widgets/inputs/DropdownInput.vue";
196197
import { SectionsOfMenuListEntries } from "@/components/widgets/floating-menus/MenuList.vue";
197198
198-
const wasm = import("@/../wasm/pkg");
199+
const wasm = import("@/../wasm/pkg").then(panicProxy);
199200
200201
const blendModeEntries: SectionsOfMenuListEntries = [
201202
[{ label: "Normal", value: BlendMode.Normal }],

frontend/src/components/widgets/floating-menus/DialogModal.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@
5757
5858
.main-column {
5959
.heading {
60-
white-space: pre;
60+
white-space: pre-wrap;
61+
max-width: 400px;
6162
margin-bottom: 4px;
6263
}
6364
6465
.details {
65-
white-space: pre;
66+
white-space: pre-wrap;
67+
max-width: 400px;
6668
}
6769
6870
.buttons-row {

frontend/src/components/widgets/inputs/MenuBarInput.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@
5454
import { defineComponent } from "vue";
5555
5656
import { comingSoon } from "@/utilities/errors";
57+
import { panicProxy } from "@/utilities/panic";
5758
5859
import IconLabel from "@/components/widgets/labels/IconLabel.vue";
5960
import { ApplicationPlatform } from "@/components/window/MainWindow.vue";
6061
import MenuList, { MenuListEntry, MenuListEntries } from "@/components/widgets/floating-menus/MenuList.vue";
6162
import { MenuDirection } from "@/components/widgets/floating-menus/FloatingMenu.vue";
6263
63-
const wasm = import("@/../wasm/pkg");
64+
const wasm = import("@/../wasm/pkg").then(panicProxy);
6465
6566
const menuEntries: MenuListEntries = [
6667
{

frontend/src/components/widgets/inputs/SwatchPairInput.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@
6969
import { defineComponent } from "vue";
7070
7171
import { rgbToDecimalRgb, RGB } from "@/utilities/color";
72+
import { panicProxy } from "@/utilities/panic";
7273
import { ResponseType, registerResponseHandler, Response, UpdateWorkingColors } from "@/utilities/response-handler";
7374
7475
import ColorPicker from "@/components/widgets/floating-menus/ColorPicker.vue";
7576
import FloatingMenu, { MenuDirection, MenuType } from "@/components/widgets/floating-menus/FloatingMenu.vue";
7677
77-
const wasm = import("@/../wasm/pkg");
78+
const wasm = import("@/../wasm/pkg").then(panicProxy);
7879
7980
export default defineComponent({
8081
components: {

frontend/src/components/widgets/options/ToolOptions.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
import { defineComponent, PropType } from "vue";
3333
3434
import { comingSoon } from "@/utilities/errors";
35+
import { panicProxy } from "@/utilities/panic";
3536
import { WidgetRow, SeparatorType, IconButtonWidget } from "@/components/widgets/widgets";
3637
3738
import Separator from "@/components/widgets/separators/Separator.vue";
3839
import IconButton from "@/components/widgets/buttons/IconButton.vue";
3940
import PopoverButton from "@/components/widgets/buttons/PopoverButton.vue";
4041
import NumberInput from "@/components/widgets/inputs/NumberInput.vue";
4142
42-
const wasm = import("@/../wasm/pkg");
43+
const wasm = import("@/../wasm/pkg").then(panicProxy);
4344
4445
export default defineComponent({
4546
props: {

frontend/src/components/window/title-bar/WindowButtonsWeb.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="window-buttons-web" @click="handleClick" :title="fullscreen.windowFullscreen ? 'Exit Fullscreen (F11)' : 'Enter Fullscreen (F11)'">
3-
<TextLabel v-if="requestFullscreenHotkeys" :italic="true">Click to access all hotkeys</TextLabel>
3+
<TextLabel v-if="requestFullscreenHotkeys" :italic="true">Go fullscreen to access all hotkeys</TextLabel>
44
<IconLabel :icon="fullscreen.windowFullscreen ? 'FullscreenExit' : 'FullscreenEnter'" />
55
</div>
66
</template>

frontend/src/utilities/documents.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
ExportDocument,
1212
SaveDocument,
1313
} from "@/utilities/response-handler";
14-
import { download, upload } from "./files";
14+
import { download, upload } from "@/utilities/files";
15+
import { panicProxy } from "@/utilities/panic";
1516

16-
const wasm = import("@/../wasm/pkg");
17+
const wasm = import("@/../wasm/pkg").then(panicProxy);
1718

1819
const state = reactive({
1920
title: "",

frontend/src/utilities/errors.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createDialog, dismissDialog } from "@/utilities/dialog";
22
import { TextButtonWidget } from "@/components/widgets/widgets";
3-
import { ResponseType, registerResponseHandler, Response, DisplayError } from "@/utilities/response-handler";
3+
import { getPanicDetails } from "@/utilities/panic";
4+
import { ResponseType, registerResponseHandler, Response, DisplayError, DisplayPanic } from "@/utilities/response-handler";
45

56
export function comingSoon(issueNumber?: number) {
67
const bugMessage = `— but you can help add it!\nSee issue #${issueNumber} on GitHub.`;
@@ -32,5 +33,74 @@ registerResponseHandler(ResponseType.DisplayError, (responseData: Response) => {
3233
};
3334
const buttons = [okButton];
3435

35-
createDialog("Warning", "Editor error", data.description, buttons);
36+
createDialog("Warning", data.title, data.description, buttons);
3637
});
38+
39+
registerResponseHandler(ResponseType.DisplayPanic, (responseData: Response) => {
40+
const data = responseData as DisplayPanic;
41+
42+
const reloadButton: TextButtonWidget = {
43+
kind: "TextButton",
44+
callback: async () => window.location.reload(),
45+
props: { label: "Reload", emphasized: true, minWidth: 96 },
46+
};
47+
const copyErrorLogButton: TextButtonWidget = {
48+
kind: "TextButton",
49+
callback: async () => navigator.clipboard.writeText(getPanicDetails()),
50+
props: { label: "Copy Error Log", emphasized: false, minWidth: 96 },
51+
};
52+
const reportOnGithubButton: TextButtonWidget = {
53+
kind: "TextButton",
54+
callback: async () => window.open(githubUrl(), "_blank"),
55+
props: { label: "Report Bug", emphasized: false, minWidth: 96 },
56+
};
57+
const buttons = [reloadButton, copyErrorLogButton, reportOnGithubButton];
58+
59+
createDialog("Warning", data.title, data.description, buttons);
60+
});
61+
62+
function githubUrl() {
63+
const url = new URL("https://github.com/GraphiteEditor/Graphite/issues/new");
64+
65+
const body = `
66+
**Describe the Crash**
67+
Explain clearly what you were doing when the crash occurred.
68+
69+
**Steps To Reproduce**
70+
Describe precisely how the crash occurred, step by step, starting with a new editor window.
71+
1. Open the Graphite Editor at https://editor.graphite.design
72+
2.
73+
3.
74+
4.
75+
5.
76+
77+
**Browser and OS*
78+
List of your browser and its version, as well as your operating system.
79+
80+
**Additional Details**
81+
Provide any further information or context that you think would be helpful in fixing the issue. Screenshots or video can be linked or attached to this issue.
82+
83+
**Stack Trace**
84+
Copied from the crash dialog in the Graphite Editor:
85+
86+
\`\`\`
87+
${getPanicDetails()}
88+
\`\`\`
89+
`.trim();
90+
91+
const fields = {
92+
title: "[Crash Report] ",
93+
body,
94+
labels: ["Crash"].join(","),
95+
projects: [].join(","),
96+
milestone: "",
97+
assignee: "",
98+
template: "",
99+
};
100+
101+
Object.entries(fields).forEach(([field, value]) => {
102+
if (value) url.searchParams.set(field, value);
103+
});
104+
105+
return url.toString();
106+
}

frontend/src/utilities/input.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { toggleFullscreen } from "@/utilities/fullscreen";
22
import { dialogIsVisible, dismissDialog, submitDialog } from "@/utilities/dialog";
3+
import { panicProxy } from "@/utilities/panic";
34

4-
const wasm = import("@/../wasm/pkg");
5+
const wasm = import("@/../wasm/pkg").then(panicProxy);
56

67
let viewportMouseInteractionOngoing = false;
78

@@ -45,10 +46,12 @@ export async function onKeyDown(e: KeyboardEvent) {
4546

4647
if (dialogIsVisible()) {
4748
if (e.key === "Escape") dismissDialog();
48-
if (e.key === "Enter") submitDialog();
49+
if (e.key === "Enter") {
50+
submitDialog();
4951

50-
// Prevent the Enter key from acting like a click on the last clicked button, which might reopen the dialog
51-
e.preventDefault();
52+
// Prevent the Enter key from acting like a click on the last clicked button, which might reopen the dialog
53+
e.preventDefault();
54+
}
5255
}
5356
}
5457

frontend/src/utilities/panic.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Import this function and chain it on all `wasm` imports like: const wasm = import("@/../wasm/pkg").then(panicProxy);
2+
// This works by proxying every function call wrapping a try-catch block to filter out redundant and confusing `RuntimeError: unreachable` exceptions sent to the console
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
export function panicProxy(module: any) {
5+
const proxyHandler = {
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
get(target: any, propKey: any, receiver: any) {
8+
const targetValue = Reflect.get(target, propKey, receiver);
9+
10+
// Keep the original value being accessed if it isn't a function or it is a class
11+
// TODO: Figure out how to also wrap (class) constructor functions instead of skipping them for now
12+
const isFunction = typeof targetValue === "function";
13+
const isClass = isFunction && /^\s*class\s+/.test(targetValue.toString());
14+
if (!isFunction || isClass) return targetValue;
15+
16+
// Replace the original function with a wrapper function that runs the original in a try-catch block
17+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, func-names
18+
return function (...args: any) {
19+
let result;
20+
try {
21+
// @ts-expect-error
22+
result = targetValue.apply(this, args);
23+
} catch (err: any) {
24+
// Suppress `unreachable` WebAssembly.RuntimeError exceptions
25+
if (!`${err}`.startsWith("RuntimeError: unreachable")) throw err;
26+
}
27+
return result;
28+
};
29+
},
30+
};
31+
32+
return new Proxy(module, proxyHandler);
33+
}
34+
35+
// Intercept console.error() for panic messages sent by code in the WASM toolchain
36+
let panicDetails = "";
37+
// eslint-disable-next-line no-console
38+
const error = console.error.bind(console);
39+
// eslint-disable-next-line no-console
40+
console.error = (...args) => {
41+
const details = "".concat(...args).trim();
42+
if (details.startsWith("panicked at")) panicDetails = details;
43+
44+
error(...args);
45+
};
46+
47+
// Get the body of the panic's exception that was printed in the console
48+
export function getPanicDetails(): string {
49+
return panicDetails;
50+
}

frontend/src/utilities/response-handler.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export enum ResponseType {
2828
SetCanvasZoom = "SetCanvasZoom",
2929
SetCanvasRotation = "SetCanvasRotation",
3030
DisplayError = "DisplayError",
31+
DisplayPanic = "DisplayPanic",
3132
DisplayConfirmationToCloseDocument = "DisplayConfirmationToCloseDocument",
3233
DisplayConfirmationToCloseAllDocuments = "DisplayConfirmationToCloseAllDocuments",
3334
}
@@ -43,8 +44,10 @@ export function handleResponse(responseType: string, responseData: any) {
4344
if (callback && data) {
4445
callback(data);
4546
} else if (data) {
47+
// eslint-disable-next-line no-console
4648
console.error(`Received a Response of type "${responseType}" but no handler was registered for it from the client.`);
4749
} else {
50+
// eslint-disable-next-line no-console
4851
console.error(`Received a Response of type "${responseType}" but but was not able to parse the data.`);
4952
}
5053
}
@@ -83,6 +86,8 @@ function parseResponse(responseType: string, data: any): Response {
8386
return newUpdateWorkingColors(data.UpdateWorkingColors);
8487
case "DisplayError":
8588
return newDisplayError(data.DisplayError);
89+
case "DisplayPanic":
90+
return newDisplayPanic(data.DisplayPanic);
8691
case "DisplayConfirmationToCloseDocument":
8792
return newDisplayConfirmationToCloseDocument(data.DisplayConfirmationToCloseDocument);
8893
case "DisplayConfirmationToCloseAllDocuments":
@@ -144,10 +149,23 @@ function newSetActiveDocument(input: any): SetActiveDocument {
144149
}
145150

146151
export interface DisplayError {
152+
title: string;
147153
description: string;
148154
}
149155
function newDisplayError(input: any): DisplayError {
150156
return {
157+
title: input.title,
158+
description: input.description,
159+
};
160+
}
161+
162+
export interface DisplayPanic {
163+
title: string;
164+
description: string;
165+
}
166+
function newDisplayPanic(input: any): DisplayPanic {
167+
return {
168+
title: input.title,
151169
description: input.description,
152170
};
153171
}

0 commit comments

Comments
 (0)