Просмотр исходного кода

添加全局错误处理器,自动刷新页面以处理特定错误和未捕获的 Promise 错误,同时优化聊天记录服务以反转简化消息列表。

wuyi 2 месяцев назад
Родитель
Сommit
10ee6269aa
2 измененных файлов с 27 добавлено и 0 удалено
  1. 24 0
      src/index.ts
  2. 3 0
      src/lib/api/chatRecordsService.ts

+ 24 - 0
src/index.ts

@@ -317,6 +317,30 @@ function setDocumentLangPackProperties(langPack: LangPackDifference.langPackDiff
   }
 }
 
+// 全局错误处理器 - 当出现特定错误时自动刷新页面
+window.addEventListener('error', (event) => {
+  const error = event.error;
+  if(error && error.message && error.message.includes('Cannot read properties of undefined (reading \'_\')')) {
+    console.warn('检测到消息过滤错误,正在刷新页面...');
+    // 延迟一点时间以确保日志输出
+    setTimeout(() => {
+      window.location.reload();
+    }, 100);
+  }
+});
+
+// 处理 Promise 中的未捕获错误
+window.addEventListener('unhandledrejection', (event) => {
+  const error = event.reason;
+  if(error && error.message && error.message.includes('Cannot read properties of undefined (reading \'_\')')) {
+    console.warn('检测到 Promise 中的消息过滤错误,正在刷新页面...');
+    // 延迟一点时间以确保日志输出
+    setTimeout(() => {
+      window.location.reload();
+    }, 100);
+  }
+});
+
 (window as any)['showIconLibrary'] = async() => {
   const {showIconLibrary} = await import('./components/iconLibrary/trigger');
   showIconLibrary();

+ 3 - 0
src/lib/api/chatRecordsService.ts

@@ -951,6 +951,9 @@ export class ChatRecordsService {
           // 处理错误,跳过此消息
         }
       }
+
+      simplifiedMessages.reverse();
+
       // 返回JSON字符串
       return JSON.stringify(simplifiedMessages);
     } catch(error) {