补齐非 console.error 类 H5 报错的观察通路

排查「H5 有输出但 Safari 看不到」时发现,即便绕开 Safari 只看 Xcode,也有三类
故障是完全静默的:

1. 资源 404(<script>/<img>/<audio> 加载失败)
   error 事件只在元素自身触发且不冒泡,原先 addEventListener('error', fn) 收不到。
   改为捕获阶段 (capture=true),JS 异常的 ev.target 是 window,据此在同一 listener
   内分流 onerror / resourceerror 两类。缺图缺 js 是 H5 最常见的线上故障,此前零信号。

2. WebView 加载失败(didFailProvisionalNavigation / didFail)
   两个 VC 的 WKNavigationDelegate 此前只实现了 didFinish。AppSchemeHandler 找不到
   index.html、子游戏 zip 解压残缺时,splash 永不淡出、卡在启动图,Xcode 无任何输出。

3. WebContent 进程终止(webViewWebContentProcessDidTerminate)
   canvas 游戏 JSC OOM 被系统回收时页面白屏,同样静默。
   ⚠️ 只 log 不自动 reload —— 自动恢复是行为变化,msext 没有,不引入。

另:H5ErrorRelay.install 增加 label 参数,输出前缀从 [H5 xxx] 变为
[H5:lobby xxx] / [H5:subGame xxx]。同时存在多个 BridgedWebView 时原先分不清
日志来自谁,也就无法反推该在 Safari「开发」菜单里选哪个条目。

无契约影响:H5 可观察行为不变,新增回调仅记录不改流程。
Debug / Release 两个配置均已编译验证。
docs/H5-Debug-Guide.md §8.1 更新能力表,新增 §8.2 已知盲区 / §8.3 设计要点。
This commit is contained in:
joywayer
2026-08-08 14:19:58 +08:00
parent f4acccdd6c
commit 7fe089577b
5 changed files with 112 additions and 21 deletions
@@ -760,5 +760,30 @@ extension WebContainerViewController: WKNavigationDelegate {
self?.splash.removeFromSuperview()
})
}
// / WebContent
// H5 AppSchemeHandler index.html
// zip splash Xcode
// WebContent canvas JSC OOM
// JS H5ErrorRelay
// log reload msext
public func webView(_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error) {
let ns = error as NSError
// -999 = NSURLErrorCancelled
diagLog("[WebView:lobby] 首次加载失败 code=\(ns.code) \(ns.localizedDescription)")
}
public func webView(_ webView: WKWebView,
didFail navigation: WKNavigation!,
withError error: Error) {
let ns = error as NSError
diagLog("[WebView:lobby] 提交后加载失败 code=\(ns.code) \(ns.localizedDescription)")
}
public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
diagLog("[WebView:lobby] ⚠️ WebContent 进程终止(多为内存不足),页面已白屏")
}
}