فهرست منبع

chore: 新增日志收集上报

zhangyuhan 2 هفته پیش
والد
کامیت
718311acda
2فایلهای تغییر یافته به همراه44 افزوده شده و 0 حذف شده
  1. 1 0
      apps/work-bench/src/sentry.js
  2. 43 0
      packages/work-bench-frame/packages/micro-frame/qiankun/store.js

+ 1 - 0
apps/work-bench/src/sentry.js

@@ -29,5 +29,6 @@ export function initSentry(Vue) {
     Sentry.setTag('url', location.href)
     const id = document.cookie.match(/(^|;)\s*ud_safe\s*=\s*([^;]+)/)
     Sentry.setUser({ id: id && id[2] })
+    window.__WORK_SENTRY__ = Sentry
   }
 }

+ 43 - 0
packages/work-bench-frame/packages/micro-frame/qiankun/store.js

@@ -3,6 +3,15 @@ import { $BRIDGE } from '../../core/bridge'
 
 let action = null
 
+// 自定义异常上报
+function sendErrorLog (content, options) {
+  if (window.Sentry && typeof window.Sentry.captureMessage === 'function') {
+    Sentry.captureMessage(content, options)
+  } else if (window.__WORK_SENTRY__ && typeof window.__WORK_SENTRY__.captureMessage === 'function') {
+    window.__WORK_SENTRY__.captureMessage(content, options)
+  }
+}
+
 export default {
   namespaced: true,
   state: () => ({
@@ -56,12 +65,46 @@ export default {
       })
 
       start({
+        async fetch(url, ...args) {
+          const isImage = /\.(png|jpe?g|gif|svg|webp|ico)$/i.test(url);
+          try {
+            return await window.fetch(url, ...args);
+          } catch (err) {
+            if (isImage) {
+              console.warn('Image fetch failed but ignored:', url);
+              sendErrorLog(new Error(`Image fetch failed but ignored`), {
+                level: 'error',
+                tags: {
+                  href: location.href,
+                  url: url
+                }
+              })
+              // 返回一个空响应或占位图,避免子应用报错
+              return new Response(new Blob(), { status: 200 });
+            }
+            console.error('Fetch failed for:', url, err);
+            sendErrorLog(new Error(`Qiankun Fetch failed`), {
+              level: 'error',
+              tags: {
+                href: location.href,
+                url: url
+              }
+            })
+            throw err;
+          }
+        },
         sandbox: {
           strictStyleIsolation: true
         }
       })
       addGlobalUncaughtErrorHandler((event) => {
         const isSubAppLoadError = event.type === 'error' && event.message.indexOf('died in status LOADING_SOURCE_CODE') !== -1
+        sendErrorLog(new Error(`SubApp Load ERROR`), {
+          level: 'error',
+          tags: {
+            href: location.href
+          }
+        })
         commit('toggleLoading', false)
         if (isSubAppLoadError) {
           commit('toggleAbnormal', {