|
7 | 7 | type DesktopUpdateCheckResult, |
8 | 8 | type DesktopUpdateState, |
9 | 9 | } from "@t3tools/contracts"; |
| 10 | +import { errorTag } from "@t3tools/shared/observability"; |
10 | 11 | import * as Cause from "effect/Cause"; |
11 | 12 | import * as Context from "effect/Context"; |
12 | 13 | import * as DateTime from "effect/DateTime"; |
@@ -34,7 +35,6 @@ import * as IpcChannels from "../ipc/channels.ts"; |
34 | 35 | import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; |
35 | 36 | import { normalizeDesktopUpdateReleaseNotes } from "./releaseNotes.ts"; |
36 | 37 | import { resolveDefaultDesktopUpdateChannel } from "./updateChannels.ts"; |
37 | | -import { describeUpdateErrorCause } from "./updateDiagnostics.ts"; |
38 | 38 | import { |
39 | 39 | createInitialDesktopUpdateState, |
40 | 40 | reduceDesktopUpdateStateOnCheckFailure, |
@@ -197,6 +197,63 @@ const { |
197 | 197 | logError: logUpdaterError, |
198 | 198 | } = DesktopObservability.makeComponentLogger("desktop-updater"); |
199 | 199 |
|
| 200 | +function redactMessage(message: string): string { |
| 201 | + // Updater HTTP errors append response bodies and headers after the first line. |
| 202 | + return (message.split(/[\r\n]/, 1)[0] ?? "") |
| 203 | + .replace(/\b(?:https?|file):\/\/[^\s"'<>]+/gi, "[redacted URL]") |
| 204 | + .replace(/\b(?:Bearer|Basic)\s+[^\s,;]+/gi, "[redacted credential]") |
| 205 | + .replace( |
| 206 | + /\b(?:authorization|proxy-authorization|cookie|set-cookie)\s*[:=].*$/gi, |
| 207 | + "[redacted header]", |
| 208 | + ) |
| 209 | + .replace( |
| 210 | + /\b[\w-]*(?:token|password|secret|credential|api[_-]?key)[\w-]*\s*[:=]\s*(?:"[^"]*"|'[^']*'|[^\s,;]+)/gi, |
| 211 | + "[redacted credential]", |
| 212 | + ) |
| 213 | + .slice(0, 512); |
| 214 | +} |
| 215 | + |
| 216 | +function summarizeCause(cause: unknown, depth: number): string { |
| 217 | + if (depth >= 3) return "[truncated cause]"; |
| 218 | + if (Cause.isCause(cause)) { |
| 219 | + return cause.reasons |
| 220 | + .slice(0, 3) |
| 221 | + .map((reason) => |
| 222 | + reason._tag === "Fail" |
| 223 | + ? summarizeCause(reason.error, depth + 1) |
| 224 | + : reason._tag === "Die" |
| 225 | + ? summarizeCause(reason.defect, depth + 1) |
| 226 | + : "Interrupted", |
| 227 | + ) |
| 228 | + .join("; "); |
| 229 | + } |
| 230 | + if (typeof cause === "string") return redactMessage(cause); |
| 231 | + if (typeof cause !== "object" || cause === null) return errorTag(cause); |
| 232 | + |
| 233 | + const error = cause as { |
| 234 | + readonly message?: unknown; |
| 235 | + readonly code?: unknown; |
| 236 | + readonly cause?: unknown; |
| 237 | + }; |
| 238 | + const code = |
| 239 | + typeof error.code === "string" && /^[A-Z][A-Z0-9_]{0,127}$/.test(error.code) |
| 240 | + ? ` [${error.code}]` |
| 241 | + : ""; |
| 242 | + const message = typeof error.message === "string" ? `: ${redactMessage(error.message)}` : ""; |
| 243 | + const nested = |
| 244 | + error.cause === undefined ? "" : `; caused by ${summarizeCause(error.cause, depth + 1)}`; |
| 245 | + return `${errorTag(cause)}${code}${message}${nested}`; |
| 246 | +} |
| 247 | + |
| 248 | +/** Log a bounded cause summary without stacks, response payloads, or request credentials. */ |
| 249 | +export function describeUpdateErrorCause(cause: unknown): string { |
| 250 | + try { |
| 251 | + return summarizeCause(cause, 0).slice(0, 1024); |
| 252 | + } catch { |
| 253 | + return "UnknownError"; |
| 254 | + } |
| 255 | +} |
| 256 | + |
200 | 257 | function parseAppUpdateYml(raw: string): Effect.Effect<Option.Option<AppUpdateYmlConfig>> { |
201 | 258 | const entries: Record<string, string> = {}; |
202 | 259 | for (const line of raw.split("\n")) { |
|
0 commit comments