Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

feat: Add error boundary

Rene Wang0f94fa996818
diff --git a/src/features/PageEditor/EditorCanvas/index.tsx b/src/features/PageEditor/EditorCanvas/index.tsx
index d761fd44ca7..556ffe30108 100644
--- a/src/features/PageEditor/EditorCanvas/index.tsx
+++ b/src/features/PageEditor/EditorCanvas/index.tsx
@@ -13,13 +13,64 @@ import {
ReactToolbarPlugin,
} from '@lobehub/editor';
import { Editor } from '@lobehub/editor/react';
-import { type CSSProperties, memo } from 'react';
+import { Alert } from '@lobehub/ui';
+import { type CSSProperties, Component, type ErrorInfo, type ReactNode, memo } from 'react';
import { useTranslation } from 'react-i18next';
import { usePageEditorStore } from '../store';
import InlineToolbar from './InlineToolbar';
import { useSlashItems } from './useSlashItems';
+interface EditorErrorBoundaryState {
+ error: Error | null;
+ hasError: boolean;
+}
+
+/**
+ * ErrorBoundary for EditorCanvas component.
+ * Catches rendering errors in the editor and displays a fallback error UI
+ * instead of crashing the entire page.
+ */
+class EditorErrorBoundary extends Component<{ children: ReactNode }, EditorErrorBoundaryState> {
+ public state: EditorErrorBoundaryState = {
+ error: null,
+ hasError: false,
+ };
+
+ public static getDerivedStateFromError(error: Error): Partial<EditorErrorBoundaryState> {
+ return { error, hasError: true };
+ }
+
+ public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
+ console.error('[EditorErrorBoundary] Caught error in editor render:', {
+ componentStack: errorInfo.componentStack,
+ error: error.message,
+ stack: error.stack,
+ });
+ }
+
+ public render() {
+ if (this.state.hasError) {
+ return (
+ <Alert
+ message={this.state.error?.message || 'An unknown error occurred in the editor'}
+ showIcon
+ style={{
+ margin: 16,
+ overflow: 'hidden',
+ position: 'relative',
+ width: '100%',
+ }}
+ title="Editor Error"
+ type="error"
+ />
+ );
+ }
+
+ return this.props.children;
+ }
+}
+
interface EditorCanvasProps {
placeholder?: string;
style?: CSSProperties;
@@ -37,45 +88,47 @@ const EditorCanvas = memo<EditorCanvasProps>(({ placeholder, style }) => {
if (!editor) return null;
return (
- <div
- onClick={(e) => {
- e.stopPropagation();
- e.preventDefault();
- }}
- >
- <Editor
- content={''}
- editor={editor!}
- lineEmptyPlaceholder={placeholder || t('pageEditor.editorPlaceholder')}
- onInit={onEditorInit}
- onTextChange={handleContentChange}
- placeholder={placeholder || t('pageEditor.editorPlaceholder')}
- plugins={[
- ReactLiteXmlPlugin,
- ReactListPlugin,
- ReactCodePlugin,
- ReactCodemirrorPlugin,
- ReactHRPlugin,
- ReactLinkPlugin,
- ReactTablePlugin,
- ReactMathPlugin,
- Editor.withProps(ReactImagePlugin, {
- defaultBlockImage: true,
- }),
- Editor.withProps(ReactToolbarPlugin, {
- children: <InlineToolbar floating />,
- }),
- ]}
- slashOption={{
- items: slashItems,
- }}
- style={{
- paddingBottom: 64,
- ...style,
+ <EditorErrorBoundary>
+ <div
+ onClick={(e) => {
+ e.stopPropagation();
+ e.preventDefault();
}}
- type={'text'}
- />
- </div>
+ >
+ <Editor
+ content={''}
+ editor={editor!}
+ lineEmptyPlaceholder={placeholder || t('pageEditor.editorPlaceholder')}
+ onInit={onEditorInit}
+ onTextChange={handleContentChange}
+ placeholder={placeholder || t('pageEditor.editorPlaceholder')}
+ plugins={[
+ ReactLiteXmlPlugin,
+ ReactListPlugin,
+ ReactCodePlugin,
+ ReactCodemirrorPlugin,
+ ReactHRPlugin,
+ ReactLinkPlugin,
+ ReactTablePlugin,
+ ReactMathPlugin,
+ Editor.withProps(ReactImagePlugin, {
+ defaultBlockImage: true,
+ }),
+ Editor.withProps(ReactToolbarPlugin, {
+ children: <InlineToolbar floating />,
+ }),
+ ]}
+ slashOption={{
+ items: slashItems,
+ }}
+ style={{
+ paddingBottom: 64,
+ ...style,
+ }}
+ type={'text'}
+ />
+ </div>
+ </EditorErrorBoundary>
);
});