Skip to content

Commit d9df33d

Browse files
committed
Add a crash handler
1 parent a42ec2c commit d9df33d

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/latest/latest.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</head>
2424

2525
<body data-bs-theme="dark">
26-
<div id="react-container">
26+
<div id="fallback-ui">
2727
<header id="header">
2828
<nav class="navbar navbar-expand bg-body-tertiary">
2929
<div class="container">
@@ -46,6 +46,7 @@ <h3><i class="bi bi-braces me-2 text-warning"></i>Please enable JavaScript</h3>
4646
</noscript>
4747
</main>
4848
</div>
49+
<div id="react-container"></div>
4950
</body>
5051

5152
</html>

src/latest/scripts/main.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import { createRoot } from "react-dom/client";
33
import { decodeHash } from "./interpreter/permalink";
44
import { ElementDataContext, parseElementData } from "./interpreter/element-data";
55
import { Theseus } from "./ui/Theseus";
6+
import { ErrorBoundary } from "react-error-boundary";
7+
import { CatastrophicFailure } from "./ui/CatastrophicFailure";
68

79
enableMapSet();
810
const root = createRoot(document.getElementById("react-container")!);
11+
const fallback = document.getElementById("fallback-ui")!;
12+
fallback.hidden = true;
913
const permalink = window.location.hash.length ? await decodeHash(window.location.hash.slice(1)) : null;
1014
if (permalink != null && !permalink.compatible) {
1115
window.location.replace(`https://vyxal.github.io/versions/v${permalink.version}#${location.hash.substring(1)}`);
@@ -14,7 +18,9 @@ if (permalink != null && !permalink.compatible) {
1418
const elementData = parseElementData(await fetch(`${DATA_URI}/theseus.json`).then((r) => r.json()));
1519
root.render(
1620
<ElementDataContext.Provider value={elementData}>
17-
<Theseus permalink={permalink?.permalink ?? null} />
21+
<ErrorBoundary fallbackRender={CatastrophicFailure}>
22+
<Theseus permalink={permalink?.permalink ?? null} />
23+
</ErrorBoundary>
1824
</ElementDataContext.Provider>,
1925
);
2026
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { FallbackProps } from "react-error-boundary";
2+
3+
export function CatastrophicFailure({ resetErrorBoundary }: FallbackProps) {
4+
return <>
5+
<header >
6+
<nav className="navbar navbar-expand bg-body-tertiary">
7+
<div className="container">
8+
<span className="navbar-brand">
9+
&nbsp;
10+
</span>
11+
</div>
12+
</nav>
13+
</header>
14+
<main className="container-sm my-auto">
15+
<div className="border border-danger bg-danger-subtle rounded p-4 mx-auto" style={{ maxWidth: "768px" }}>
16+
<h3><i className="bi bi-exclamation-circle-fill me-2 text-danger"></i>Catastrophic failure</h3>
17+
<p>
18+
The interpreter has crashed. Sorry about that! Don't worry, your code has been preserved.
19+
If you can reproduce this crash, please open an issue
20+
on <a href="https://github.com/vyxal/vyxal.github.io/issues/">the GitHub repository</a>.
21+
</p>
22+
<button className="btn btn-danger" onClick={resetErrorBoundary}>Restart</button>
23+
</div>
24+
</main>
25+
</>;
26+
}

src/latest/scripts/ui/Theseus.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function Theseus({ permalink }: TheseusProps) {
153153
}
154154
}, [code, flags, inputs, timeout, runnerRef, state]);
155155

156+
throw new Error("frob")
156157
return <>
157158
<SettingsDialog
158159
settings={settings}

0 commit comments

Comments
 (0)