Repository navigation
Expand file tree
/
Copy pathcrashReporting.ts
More file actions
407 lines (375 loc) · 11.3 KB
/
Copy pathcrashReporting.ts
File metadata and controls
407 lines (375 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import i18n from "@/i18n";
import logger from "./logger";
import { getPendingCrashReports } from "./diagnostics";
export type CrashSource =
| "window"
| "unhandled-rejection"
| "react"
| "api"
| "backend-panic"
| "startup";
export type ApiFailureKind =
| "clock_skew"
| "signature"
| "auth"
| "network"
| "server"
| "parse"
| "device_fingerprint"
| "unknown";
export interface ApiHealthSnapshot {
blocked: boolean;
consecutive_failures: number;
kind: ApiFailureKind | null;
message: string | null;
clock_skew_seconds: number | null;
last_failure_at: string | null;
}
export interface BackendCrashEvent {
message: string;
location?: string | null;
thread: string;
backtrace: string;
report_path?: string | null;
timestamp: string;
}
export interface CrashReport {
id: number;
title: string;
message: string;
detail?: string;
stack?: string;
source: CrashSource;
timestamp: string;
fatal: boolean;
api?: ApiHealthSnapshot;
crashPaths?: string[];
}
interface CrashInput {
title: string;
message: string;
detail?: string;
stack?: string;
source: CrashSource;
fatal?: boolean;
api?: ApiHealthSnapshot;
crashPaths?: string[];
}
export interface CrashDiagnosis {
causeKey: string;
suggestionKeys: string[];
}
let currentReport: CrashReport | null = null;
let reportCounter = 0;
let lastSignature = "";
let lastSignatureAt = 0;
let handlersInstalled = false;
const listeners = new Set<() => void>();
function notifyListeners() {
listeners.forEach((listener) => listener());
}
export function subscribeCrashReports(listener: () => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
export function getCurrentCrashReport(): CrashReport | null {
return currentReport;
}
export function showCrashReport(input: CrashInput): CrashReport {
const report: CrashReport = {
id: ++reportCounter,
title: input.title,
message: input.message,
detail: input.detail,
stack: input.stack,
source: input.source,
timestamp: new Date().toISOString(),
fatal: input.fatal ?? false,
api: input.api,
crashPaths: input.crashPaths,
};
currentReport = report;
notifyListeners();
return report;
}
export function dismissCrashReport() {
currentReport = null;
notifyListeners();
}
/** 记录并展示一个错误。相同错误 2 秒内只弹一次,避免级联报错刷屏。 */
export function reportCrash(input: CrashInput) {
logger.error(
`[Crash][${input.source}] ${input.title}: ${input.message}`,
{ detail: input.detail, stack: input.stack },
"Crash",
);
const signature = `${input.source}|${input.message}`;
const now = Date.now();
if (signature === lastSignature && now - lastSignatureAt < 2000) {
if (!currentReport) showCrashReport(input);
return;
}
lastSignature = signature;
lastSignatureAt = now;
showCrashReport(input);
}
function handleApiBlocked(snapshot: ApiHealthSnapshot) {
// 停止启动进度等待并收起 Splash,让应用可以「不获取任何账户数据也进入」。
void import("./startupProgress").then(({ abortStartup }) => {
abortStartup(i18n.t("crash.api_title"));
});
// 账户数据获取频繁失败:标记账户不可用,账户页展示「账号不可用 / 重新登录」。
void import("./accountAvailability").then(({ reasonFromApiKind, setAccountUnavailable }) => {
setAccountUnavailable(reasonFromApiKind(snapshot.kind), snapshot.message ?? "");
});
if (currentReport?.source === "api") return;
const isFingerprint = snapshot.kind === "device_fingerprint";
const body = isFingerprint
? i18n.t("crash.api_fingerprint_body", {
count: snapshot.consecutive_failures,
})
: i18n.t("crash.api_body", { count: snapshot.consecutive_failures });
// 非致命:用户可以直接关闭弹窗继续使用应用(只是没有账户数据)。
showCrashReport({
title: i18n.t("crash.api_title"),
message: snapshot.message || body,
detail: body,
source: "api",
fatal: false,
api: snapshot,
});
}
async function checkApiHealth() {
try {
const snapshot = await invoke<ApiHealthSnapshot>("get_api_health");
if (snapshot.blocked) handleApiBlocked(snapshot);
} catch (error) {
logger.warn("Failed to query API health: " + error, "Crash");
}
}
/**
* 浏览器把 ResizeObserver 的告警也当作 window error 派发(Chromium / WebView2 行为):
* 观察回调在交付周期里写布局时就会报一次,页面本身没有出错。
* 这类消息必须挡在弹窗之外——否则用户会看到一个错因完全对不上的崩溃弹窗
*(曾被启发式判成「服务端持续返回错误」),只能靠重新加载关掉。
*/
const BENIGN_WINDOW_ERROR = /resizeobserver loop/i;
/** 是否为浏览器噪音(非故障)错误。 */
function isBenignWindowError(message: string): boolean {
return BENIGN_WINDOW_ERROR.test(message);
}
/** 安装全局 JS 错误、后端崩溃与 API 熔断事件监听。 */
export function installCrashHandlers() {
if (handlersInstalled || typeof window === "undefined") return;
handlersInstalled = true;
window.addEventListener("error", (event) => {
const error = event.error instanceof Error ? event.error : null;
const message = error?.message || event.message || "Unknown error";
if (isBenignWindowError(message)) {
logger.debug(`[Crash] ignored benign window error: ${message}`, undefined, "Crash");
return;
}
reportCrash({
title: i18n.t("crash.title"),
message,
stack: error?.stack,
detail: event.filename
? `${event.filename}:${event.lineno}:${event.colno}`
: undefined,
source: "window",
});
});
window.addEventListener("unhandledrejection", (event) => {
const reason = event.reason;
const error = reason instanceof Error ? reason : null;
const message = error?.message || String(reason ?? "Unhandled promise rejection");
if (isBenignWindowError(message)) {
logger.debug(`[Crash] ignored benign rejection: ${message}`, undefined, "Crash");
return;
}
reportCrash({
title: i18n.t("crash.title"),
message,
stack: error?.stack,
source: "unhandled-rejection",
});
});
void listen<ApiHealthSnapshot>("app:api-health", (event) => {
if (event.payload?.blocked) handleApiBlocked(event.payload);
}).catch((error) => {
logger.warn("Failed to listen for API health events: " + error, "Crash");
});
void listen<BackendCrashEvent>("app:crash", (event) => {
const payload = event.payload;
reportCrash({
title: i18n.t("crash.backend_crash"),
message: payload.message,
detail: payload.location ?? undefined,
stack: payload.backtrace,
source: "backend-panic",
fatal: true,
crashPaths: payload.report_path ? [payload.report_path] : undefined,
});
}).catch((error) => {
logger.warn("Failed to listen for backend crash events: " + error, "Crash");
});
// 事件监听可能晚于后端失败,启动阶段额外轮询两次作为兜底。
window.setTimeout(() => {
void checkApiHealth();
}, 1500);
window.setTimeout(() => {
void checkApiHealth();
}, 6000);
}
/** 载入上次运行留下的未读崩溃报告。 */
export async function loadPendingCrashReports() {
if (currentReport) return;
try {
const reports = await getPendingCrashReports();
if (!reports.length || currentReport) return;
const latest = reports[0];
showCrashReport({
title: i18n.t("crash.pending_crash"),
message: latest.message,
detail: i18n.t("crash.pending_crash_hint"),
stack: latest.content,
source: "backend-panic",
fatal: false,
crashPaths: reports.map((report) => report.path),
});
} catch (error) {
logger.warn("Failed to load pending crash reports: " + error, "Crash");
}
}
export function diagnoseCrash(report: CrashReport): CrashDiagnosis {
const api = report.api;
if (api) {
if (api.kind === "device_fingerprint") {
return {
causeKey: "crash.cause_fingerprint",
suggestionKeys: ["crash.suggestion_fingerprint"],
};
}
if (
api.kind === "clock_skew" ||
(api.clock_skew_seconds != null &&
Math.abs(api.clock_skew_seconds) >= 180)
) {
return {
causeKey: "crash.cause_clock",
suggestionKeys: ["crash.suggestion_clock"],
};
}
if (api.kind === "signature") {
return {
causeKey: "crash.cause_clock",
suggestionKeys: ["crash.suggestion_clock", "crash.suggestion_network"],
};
}
if (api.kind === "auth") {
return {
causeKey: "crash.cause_auth",
suggestionKeys: ["crash.suggestion_auth"],
};
}
if (api.kind === "network") {
return {
causeKey: "crash.cause_network",
suggestionKeys: ["crash.suggestion_network"],
};
}
if (api.kind === "parse") {
return {
causeKey: "crash.cause_data",
suggestionKeys: ["crash.suggestion_data"],
};
}
if (api.kind === "server" || api.kind === "unknown") {
return {
causeKey: "crash.cause_api",
suggestionKeys: ["crash.suggestion_api"],
};
}
}
const text = `${report.message} ${report.detail ?? ""} ${report.stack ?? ""}`.toLowerCase();
if (
/signature|sign |timestamp|clock|time skew|时间|签名|请求过期|expired/.test(
text,
)
) {
return {
causeKey: "crash.cause_clock",
suggestionKeys: ["crash.suggestion_clock"],
};
}
if (
/http request failed|timed out|timeout|connect|dns|network|econn|fetch failed|网络|连接/.test(
text,
)
) {
return {
causeKey: "crash.cause_network",
suggestionKeys: ["crash.suggestion_network"],
};
}
if (
/cred|token|401|403|auth|unauthorized|登录|认证|凭证/.test(text)
) {
return {
causeKey: "crash.cause_auth",
suggestionKeys: ["crash.suggestion_auth"],
};
}
if (
/access is denied|os error 5|permission|no space|拒绝访问|权限|磁盘/.test(
text,
)
) {
return {
causeKey: "crash.cause_permission",
suggestionKeys: ["crash.suggestion_permission"],
};
}
if (
/failed to parse|parse json|serde|invalid data|corrupt|配置损坏|解析/.test(
text,
)
) {
return {
causeKey: "crash.cause_data",
suggestionKeys: ["crash.suggestion_data"],
};
}
if (/api error|code=|server|500|502|503|服务端/.test(text)) {
return {
causeKey: "crash.cause_api",
suggestionKeys: ["crash.suggestion_api"],
};
}
return {
causeKey: "crash.cause_unknown",
suggestionKeys: ["crash.suggestion_unknown"],
};
}
export function formatCrashReport(report: CrashReport): string {
const lines = [
"EndProtocol crash report",
`Time: ${report.timestamp}`,
`Source: ${report.source}`,
`Title: ${report.title}`,
`Message: ${report.message}`,
];
if (report.detail) lines.push(`Detail: ${report.detail}`);
if (report.stack) lines.push("", "Stack / raw details:", report.stack);
if (report.api) {
lines.push("", "API health:", JSON.stringify(report.api, null, 2));
}
if (report.crashPaths?.length) {
lines.push("", `Crash files: ${report.crashPaths.join(", ")}`);
}
return lines.join("\n");
}