Phase 9.2:H5ErrorRelay 把 H5 异常中继到原生 print

零修改 H5(原则 A):WKUserScript .atDocumentStart 在业务 JS 跑前 hook
三类异常源:
- `console.error(...)`:保留原函数链式调用 + 转发 args
- `window.onerror`:onmessage / filename / lineno / colno / error.stack
- `unhandledrejection`:reason + reason.stack

走独立的 webkit.messageHandlers.h5error 通道(与 WVJB 不同 channel,不冲突)。
原生侧 MessageProxy weak target 切断 retain cycle。仅 BridgedWebView 安装
(大厅+子游戏),OverlayViewController 是第三方外链,不挂。

开发期价值:H5 出错时 Xcode console 直接能看到 "[H5 onerror] ... at xxx.js:42:8",
减少调试盲点。生产期 print 走 OSLog,对体积/性能影响极小。

新增:
- Source/Bridge/H5ErrorRelay.swift(@MainActor singleton)

接入:
- BridgedWebView.init:H5ErrorRelay.shared.install(into:) 在 WVJB 之后

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
joywayer
2026-06-23 02:04:46 +08:00
co-authored by Claude Opus 4.7
parent 5885f97715
commit a1c5883cc5
3 changed files with 129 additions and 2 deletions
+2 -2
View File
@@ -715,7 +715,7 @@ H5 调 `OpenurlTitleData` 打开弹层 WebView,弹层内 H5 用 `window.settin
- [ ] **9.1** Sentry-Cocoa SPM 依赖 + `Source/Analytics/SentryCrashReporter.swift`
- 编译开关 `SENTRY_ENABLED`(默认 ON+ `NoopCrashReporter` fallback
- DSN 通过 xcconfig 注入,不入 git
- [ ] **9.2** `Source/Bridge/Handlers/H5ErrorRelay.swift`注入 `window.onerror` / `unhandledrejection` polyfill + `reportH5Error` 桥 handlerDesign §11.4,新增 handler 不破坏契约)
- [x] **9.2** `Source/Bridge/H5ErrorRelay.swift`WKUserScript .atDocumentStart 注入 hook,拦截 `console.error` / `window.onerror` / `unhandledrejection`,通过独立的 `webkit.messageHandlers.h5error` 桥(与 WVJB 不同 channel)转发到原生 print。仅 BridgedWebView 安装(大厅+子游戏),Overlay 第三方外链不挂。零修改 H5。
- [ ] **9.3** 极光(JAnalytics):保持 `NoopAnalytics` stub**ADR-005 决策**
- `Source/Analytics/Tracker.swift` 协议 + `NoopAnalytics` 实现
- `JAnalyticsTracker.swift` 留蓝图骨架(`#if JANALYTICS_ENABLED` 包裹,默认 OFF
@@ -972,7 +972,7 @@ H5 调 `OpenurlTitleData` 打开弹层 WebView,弹层内 H5 用 `window.settin
### Phase 9 SDK 真实化 + 监控
- [ ] 9.1 Sentry SPM + CrashReporter
- [ ] 9.2 H5ErrorRelay
- [x] 9.2 H5ErrorRelay(独立 webkit.messageHandlers.h5errorBridgedWebView 安装,Overlay 不挂)
- [ ] 9.3 极光保持 NoopAnalyticsJANALYTICS_ENABLED OFF
- [ ] 9.4 闲聊保持 NoopSharePlatform
- [ ] 9.5 Agora 保持 NoopVideoRoomAGORA_ENABLED OFF