Skip to content

Commit 1c487cc

Browse files
Ahmed RAhmed R
authored andcommitted
Fix crash when rendering unsupported code fence languages (#266)
The app crashed when displaying code blocks with language identifiers not bundled in Shiki (e.g. "env" from .env file edits). The language string was cast to SupportedLanguages without validation, causing resolveLanguage to throw an unhandled error that killed the thread. Three layers of defense added: - getHighlighterPromise falls back to "text" when Shiki rejects - codeToHtml call wrapped in try/catch with "text" fallback - CodeHighlightErrorBoundary prevents component tree crashes Closes #266
1 parent 5a90b6b commit 1c487cc

1 file changed

Lines changed: 48 additions & 13 deletions

File tree

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type SupportedLanguages,
55
} from "@pierre/diffs";
66
import { CheckIcon, CopyIcon } from "lucide-react";
7-
import {
7+
import React, {
88
Children,
99
Suspense,
1010
isValidElement,
@@ -28,6 +28,27 @@ import { resolveMarkdownFileLinkTarget } from "../markdown-links";
2828
import { readNativeApi } from "../nativeApi";
2929
import { preferredTerminalEditor } from "../terminal-links";
3030

31+
class CodeHighlightErrorBoundary extends React.Component<
32+
{ fallback: ReactNode; children: ReactNode },
33+
{ hasError: boolean }
34+
> {
35+
constructor(props: { fallback: ReactNode; children: ReactNode }) {
36+
super(props);
37+
this.state = { hasError: false };
38+
}
39+
40+
static getDerivedStateFromError() {
41+
return { hasError: true };
42+
}
43+
44+
override render() {
45+
if (this.state.hasError) {
46+
return this.props.fallback;
47+
}
48+
return this.props.children;
49+
}
50+
}
51+
3152
interface ChatMarkdownProps {
3253
text: string;
3354
cwd: string | undefined;
@@ -99,6 +120,14 @@ function getHighlighterPromise(language: string): Promise<DiffsHighlighter> {
99120
themes: [resolveDiffThemeName("dark"), resolveDiffThemeName("light")],
100121
langs: [language as SupportedLanguages],
101122
preferredHighlighter: "shiki-js",
123+
}).catch((err) => {
124+
highlighterPromiseCache.delete(language);
125+
if (language === "text") {
126+
// "text" itself failed — Shiki cannot initialize at all, surface the error
127+
throw err;
128+
}
129+
// Language not supported by Shiki — fall back to "text"
130+
return getHighlighterPromise("text");
102131
});
103132
highlighterPromiseCache.set(language, promise);
104133
return promise;
@@ -179,10 +208,14 @@ function SuspenseShikiCodeBlock({
179208
}
180209

181210
const highlighter = use(getHighlighterPromise(language));
182-
const highlightedHtml = useMemo(
183-
() => highlighter.codeToHtml(code, { lang: language, theme: themeName }),
184-
[code, highlighter, language, themeName],
185-
);
211+
const highlightedHtml = useMemo(() => {
212+
try {
213+
return highlighter.codeToHtml(code, { lang: language, theme: themeName });
214+
} catch {
215+
// If highlighting fails for this language, render as plain text
216+
return highlighter.codeToHtml(code, { lang: "text", theme: themeName });
217+
}
218+
}, [code, highlighter, language, themeName]);
186219

187220
useEffect(() => {
188221
if (!isStreaming) {
@@ -235,14 +268,16 @@ function ChatMarkdown({ text, cwd, isStreaming = false }: ChatMarkdownProps) {
235268

236269
return (
237270
<MarkdownCodeBlock code={codeBlock.code}>
238-
<Suspense fallback={<pre {...props}>{children}</pre>}>
239-
<SuspenseShikiCodeBlock
240-
className={codeBlock.className}
241-
code={codeBlock.code}
242-
themeName={diffThemeName}
243-
isStreaming={isStreaming}
244-
/>
245-
</Suspense>
271+
<CodeHighlightErrorBoundary fallback={<pre {...props}>{children}</pre>}>
272+
<Suspense fallback={<pre {...props}>{children}</pre>}>
273+
<SuspenseShikiCodeBlock
274+
className={codeBlock.className}
275+
code={codeBlock.code}
276+
themeName={diffThemeName}
277+
isStreaming={isStreaming}
278+
/>
279+
</Suspense>
280+
</CodeHighlightErrorBoundary>
246281
</MarkdownCodeBlock>
247282
);
248283
},

0 commit comments

Comments
 (0)