A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(editor): isolate panel render crashes with an error boundary
esengine07a814d82482
diff --git a/desktop/src/components/ErrorBoundary.tsx b/desktop/src/components/ErrorBoundary.tsxnew file mode 100644index 000000000..0031cb951--- /dev/null+++ b/desktop/src/components/ErrorBoundary.tsx@@ -0,0 +1,71 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file ErrorBoundary.tsx — crash isolation for a dock panel. A render error in+ * one panel renders a recoverable card here instead of unmounting the whole+ * editor tree, and auto-clears on the next Vite HMR update so fixing the+ * offending code restores the panel without a full page reload.+ */+import { Component, type ErrorInfo, type ReactNode, type CSSProperties } from 'react';++interface Props {+ /** Human label for the crashed region, shown in the fallback. */+ label?: string;+ children: ReactNode;+}++interface State {+ error: Error | null;+}++export class ErrorBoundary extends Component<Props, State> {+ state: State = { error: null };+ private offHot?: () => void;++ static getDerivedStateFromError(error: Error): State {+ return { error };+ }++ componentDidCatch(error: Error, info: ErrorInfo) {+ const where = this.props.label ? ` [${this.props.label}]` : '';+ console.error(`Panel${where} render crashed:`, error, info.componentStack);+ }++ componentDidMount() {+ // Vite fires 'vite:afterUpdate' after applying an HMR patch. Clearing the+ // error then lets the (now-fixed) child re-render; if it still throws, the+ // boundary simply catches again.+ if (import.meta.hot) {+ const reset = () => this.setState((s) => (s.error ? { error: null } : s));+ import.meta.hot.on('vite:afterUpdate', reset);+ this.offHot = () => import.meta.hot?.off('vite:afterUpdate', reset);+ }+ }++ componentWillUnmount() {+ this.offHot?.();+ }++ private reset = () => this.setState({ error: null });++ render() {+ const { error } = this.state;+ if (!error) return this.props.children;+ return (+ <div style={S.root} role="alert">+ <div style={S.title}>⚠ {this.props.label ?? 'This panel'} hit an error</div>+ <div style={S.msg}>{error.message}</div>+ <button type="button" style={S.btn} onClick={this.reset}>+ Reload panel+ </button>+ </div>+ );+ }+}++const S: Record<string, CSSProperties> = {+ root: { display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 10, height: '100%', padding: 16, textAlign: 'center', color: 'var(--text, #cbd2da)' },+ title: { fontSize: 13, fontWeight: 600, color: 'var(--danger, #e06c75)' },+ msg: { fontSize: 12, opacity: 0.75, maxWidth: 480, wordBreak: 'break-word', fontFamily: 'var(--mono, ui-monospace, monospace)' },+ btn: { padding: '4px 12px', fontSize: 12, borderRadius: 4, border: '1px solid var(--border, #333)', background: 'var(--panel, #232830)', color: 'inherit', cursor: 'pointer' },+};diff --git a/desktop/src/layout/DockLayout.tsx b/desktop/src/layout/DockLayout.tsxindex 98577cebf..9184e05bc 100644--- a/desktop/src/layout/DockLayout.tsx+++ b/desktop/src/layout/DockLayout.tsx@@ -22,9 +22,14 @@ import { StateMachineEditor } from '@/panels/StateMachineEditor';import { BtTreeEditor } from '@/panels/BtTreeEditor';import { ProfilerPanel } from '@/panels/ProfilerPanel';import { Perf } from '@/components/Perf';+import { ErrorBoundary } from '@/components/ErrorBoundary';import { dockApi } from '@/layout/dockApi';-const panel = (id: string, node: ReactNode) => <Perf id={id}>{node}</Perf>;+const panel = (id: string, node: ReactNode) => (+ <Perf id={id}>+ <ErrorBoundary label={id}>{node}</ErrorBoundary>+ </Perf>+);// Each dock panel is a thin wrapper so dockview owns mount/unmount.const components: Record<string, FC<IDockviewPanelProps>> = {@@ -39,7 +44,7 @@ const components: Record<string, FC<IDockviewPanelProps>> = {materialgraph: () => panel('materialgraph', <MaterialGraphEditor />),statemachine: () => panel('statemachine', <StateMachineEditor />),behaviortree: () => panel('behaviortree', <BtTreeEditor />),- profiler: () => <ProfilerPanel />,+ profiler: () => <ErrorBoundary label="profiler"><ProfilerPanel /></ErrorBoundary>,// The "Game" view (isolated play realm) — added on Play, removed on Stop.game: () => panel('game', <GamePanel />),};