Skip to content

Commit 1d1bae9

Browse files
committed
Add error boundary for preview panel
1 parent 8f37b45 commit 1d1bae9

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/app/components/generator/PreviewPanel.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
11
import type { DocAndNode } from '@spyglassmc/core'
2+
import { useErrorBoundary } from 'preact/hooks'
23
import { useDocAndNode } from '../../contexts/Spyglass.jsx'
34
import { useVersion } from '../../contexts/Version.jsx'
45
import { checkVersion } from '../../services/index.js'
56
import { safeJsonParse } from '../../Utils.js'
7+
import { ErrorPanel } from '../ErrorPanel.jsx'
68
import { BiomeSourcePreview, BlockStatePreview, DecoratorPreview, DensityFunctionPreview, LootTablePreview, ModelPreview, NoisePreview, NoiseSettingsPreview, RecipePreview, StructureSetPreview } from '../previews/index.js'
79

810
export const HasPreview = ['loot_table', 'recipe', 'dimension', 'worldgen/density_function', 'worldgen/noise', 'worldgen/noise_settings', 'worldgen/configured_feature', 'worldgen/placed_feature', 'worldgen/structure_set', 'block_definition', 'docAndNode']
911

1012
type PreviewPanelProps = {
11-
docAndNode: DocAndNode | undefined,
1213
id: string,
14+
docAndNode: DocAndNode | undefined,
1315
shown: boolean,
14-
onError: (message: string) => unknown,
1516
}
16-
export function PreviewPanel({ docAndNode: original, id, shown }: PreviewPanelProps) {
17-
const { version } = useVersion()
18-
17+
export function PreviewPanel({ id, docAndNode: original, shown }: PreviewPanelProps) {
1918
if (!original) return <></>
2019

2120
const docAndNode = useDocAndNode(original)
2221

22+
const [error, dismissError] = useErrorBoundary()
23+
24+
if (error) {
25+
const previewError = new Error(`Preview error: ${error.message}`)
26+
if (error.stack) {
27+
previewError.stack = error.stack
28+
}
29+
return <ErrorPanel error={previewError} onDismiss={dismissError} />
30+
}
31+
32+
return <div>
33+
<PreviewContent key={id} id={id} docAndNode={docAndNode} shown={shown} />
34+
</div>
35+
}
36+
37+
type PreviewContentProps = {
38+
docAndNode: DocAndNode,
39+
id: string,
40+
shown: boolean,
41+
}
42+
export function PreviewContent({ id, docAndNode, shown }: PreviewContentProps) {
43+
const { version } = useVersion()
44+
2345
if (id === 'loot_table') {
2446
return <LootTablePreview {...{ docAndNode, shown }} />
2547
}

src/app/components/generator/SchemaGenerator.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
3232
const [error, setError] = useState<Error | string | null>(null)
3333
const [errorBoundary, errorRetry] = useErrorBoundary()
3434
if (errorBoundary) {
35-
errorBoundary.message = `Something went wrong rendering the generator: ${errorBoundary.message}`
36-
return <main><ErrorPanel error={errorBoundary} onDismiss={errorRetry} /></main>
35+
const generatorError = new Error(`Generator error: ${errorBoundary.message}`)
36+
if (errorBoundary.stack) {
37+
generatorError.stack = errorBoundary.stack
38+
}
39+
return <main><ErrorPanel error={generatorError} onDismiss={errorRetry} /></main>
3740
}
3841

3942
useEffect(() => Store.visitGenerator(gen.id), [gen.id])
@@ -405,7 +408,7 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
405408
</div>
406409
</div>
407410
<div class={`popup-preview${previewShown ? ' shown' : ''}`}>
408-
<PreviewPanel docAndNode={docAndNode} id={gen.id} shown={previewShown} onError={setError} />
411+
<PreviewPanel docAndNode={docAndNode} id={gen.id} shown={previewShown} />
409412
</div>
410413
<div class={`popup-source${sourceShown ? ' shown' : ''}`}>
411414
<SourcePanel docAndNode={docAndNode} {...{doCopy, doDownload, doImport}} copySuccess={copySuccess} onError={setError} />

0 commit comments

Comments
 (0)