Skip to content

Commit 572cdb6

Browse files
feat: use error boundaries for Markdown issues
1 parent 2638411 commit 572cdb6

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/lib/components/MarkdownRenderer.svelte

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import Markdown, { type Plugin } from "svelte-exmarkdown";
1010
import { gfmPlugin } from "svelte-exmarkdown/gfm";
1111
import rehypeRaw from "rehype-raw";
12+
import posthog from "posthog-js";
1213
import { cn } from "$lib/utils";
14+
import { Button } from "$lib/components/ui/button";
1315
1416
type Props = {
1517
markdown: string;
@@ -38,12 +40,44 @@
3840
className
3941
)}
4042
>
41-
<Markdown
42-
{md}
43-
plugins={[
44-
gfmPlugin(),
45-
...(parseRawHtml ? [{ rehypePlugin: rehypeRaw }] : []),
46-
...additionalPlugins
47-
]}
48-
/>
43+
<svelte:boundary onerror={e => posthog.captureException(e, { caughtInBoundary: true })}>
44+
{#snippet failed(error, reset)}
45+
{@const message = (() => {
46+
if (error instanceof Error) return error.message.trim();
47+
return `${error}`;
48+
})()}
49+
{#if !inline}
50+
<div
51+
class="flex flex-col rounded-xl border-[0.5px] border-primary bg-red-500/25 px-5 pt-3 pb-4"
52+
>
53+
<span>An error occurred while rendering this Markdown content:</span>
54+
<pre
55+
class="mt-2 mb-4 rounded-lg bg-neutral-800 px-3 py-2 whitespace-pre-line outline outline-neutral-600">
56+
{message}
57+
</pre>
58+
<span>
59+
It's now rendered with a minimal look to avoid further errors. Please <a
60+
href="https://github.com/WarningImHack3r/svelte-changelog/issues"
61+
target="_blank"
62+
>
63+
report this issue
64+
</a> if it's not already known.
65+
</span>
66+
<Button variant="outline" class="ml-auto border-primary/50 bg-red-300/30" onclick={reset}>
67+
Retry
68+
</Button>
69+
</div>
70+
{/if}
71+
<Markdown {md} />
72+
{/snippet}
73+
74+
<Markdown
75+
{md}
76+
plugins={[
77+
gfmPlugin(),
78+
...(parseRawHtml ? [{ rehypePlugin: rehypeRaw }] : []),
79+
...additionalPlugins
80+
]}
81+
/>
82+
</svelte:boundary>
4983
</svelte:element>

0 commit comments

Comments
 (0)