[P1/优化] ErrorBoundary 未上报错误到主进程,无法集中排查 #49

Closed
opened 2026-07-21 21:56:14 +08:00 by thzxx · 1 comment
Owner

问题类型

优化 / 高 / 前端组件

文件位置

src/components/common/ErrorBoundary.tsx

问题描述

ErrorBoundary 捕获渲染错误后:

  1. 显示 fallback UI
  2. 调用 console.error

但未将错误上报到主进程:

  • 无法记录到 electron-log
  • 无法在审计日志中追踪
  • 无法集中分析
  • 用户描述问题时缺乏上下文

影响

  • 错误排查困难
  • 用户报告时缺乏日志

建议修复

class ErrorBoundary extends React.Component {
  componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
    // 上报到主进程
    window.metonaAPI?.reportError({
      type: 'react_error_boundary',
      error: error.message,
      stack: error.stack,
      componentStack: errorInfo.componentStack,
      timestamp: Date.now(),
    });

    // 同时 console.error 保留
    console.error('ErrorBoundary caught:', error, errorInfo);
  }
}

主进程添加 IPC:

ipcMain.handle('error:report', (event, payload) => {
  log.error('[Frontend Error]', payload);
  auditService.log({ type: 'frontend_error', payload });
});

可选集成 Sentry 等错误监控服务。

## 问题类型 优化 / 高 / 前端组件 ## 文件位置 `src/components/common/ErrorBoundary.tsx` ## 问题描述 ErrorBoundary 捕获渲染错误后: 1. 显示 fallback UI 2. 调用 console.error 但未将错误上报到主进程: - 无法记录到 electron-log - 无法在审计日志中追踪 - 无法集中分析 - 用户描述问题时缺乏上下文 ## 影响 - 错误排查困难 - 用户报告时缺乏日志 ## 建议修复 ```tsx class ErrorBoundary extends React.Component { componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { // 上报到主进程 window.metonaAPI?.reportError({ type: 'react_error_boundary', error: error.message, stack: error.stack, componentStack: errorInfo.componentStack, timestamp: Date.now(), }); // 同时 console.error 保留 console.error('ErrorBoundary caught:', error, errorInfo); } } ``` 主进程添加 IPC: ```ts ipcMain.handle('error:report', (event, payload) => { log.error('[Frontend Error]', payload); auditService.log({ type: 'frontend_error', payload }); }); ``` 可选集成 Sentry 等错误监控服务。
thzxx added the ??????? labels 2026-07-21 21:56:14 +08:00
Author
Owner

修复说明

文件: src/components/common/ErrorBoundary.tsx + electron/preload.ts

修复: preload 暴露 app.reportError(通过 ipcRenderer.send('error:report', payload) 单向发送);ErrorBoundary 的 componentDidCatch 中调用上报(含 type/error/stack/componentStack/timestamp),同时保留 console.error

验证: tsc --noEmit 类型检查通过。

## 修复说明 **文件**: `src/components/common/ErrorBoundary.tsx` + `electron/preload.ts` **修复**: preload 暴露 `app.reportError`(通过 `ipcRenderer.send('error:report', payload)` 单向发送);ErrorBoundary 的 `componentDidCatch` 中调用上报(含 type/error/stack/componentStack/timestamp),同时保留 `console.error`。 **验证**: `tsc --noEmit` 类型检查通过。
thzxx closed this issue 2026-07-22 09:43:32 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MetonaTeam/metona-ai-desktop#49