Skip to content

Commit 1c50504

Browse files
committed
refactor(desktop): keep updater diagnostics in the update module
1 parent d64de0a commit 1c50504

4 files changed

Lines changed: 119 additions & 122 deletions

File tree

‎apps/desktop/src/updates/DesktopUpdates.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,64 @@ describe("DesktopUpdates", () => {
927927
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
928928
});
929929
});
930+
931+
describe("describeUpdateErrorCause", () => {
932+
it("preserves network and nested failure reasons", () => {
933+
const networkError = Object.assign(new Error("getaddrinfo ENOTFOUND updates.example.com"), {
934+
code: "ENOTFOUND",
935+
});
936+
const cause = new Error("fetch failed", { cause: networkError });
937+
938+
assert.equal(
939+
DesktopUpdates.describeUpdateErrorCause(Cause.fail(cause)),
940+
"Error: fetch failed; caused by Error [ENOTFOUND]: getaddrinfo ENOTFOUND updates.example.com",
941+
);
942+
assert.include(DesktopUpdates.describeUpdateErrorCause(Cause.die(networkError)), "[ENOTFOUND]");
943+
});
944+
945+
it("removes URLs, credentials, response bodies, and headers from summaries", () => {
946+
const messages = [
947+
"Cannot download https://user:[email protected]/update?token=query-secret, status 403",
948+
"Request failed: Bearer bearer-secret",
949+
"Request failed: Basic basic-secret",
950+
"Request failed: Authorization: Bearer header-secret",
951+
"Request failed: Cookie=session-secret",
952+
"Request failed: api_key=\"key-secret\" password='password-secret' access_token=token-secret",
953+
'404 Not Found\n{"body":"body-secret"}\nHeaders: cookie=header-secret',
954+
"Request failed\rHeaders: cookie=header-secret",
955+
];
956+
957+
for (const message of messages) {
958+
const summary = DesktopUpdates.describeUpdateErrorCause(new Error(message));
959+
assert.include(summary, "Error:");
960+
assert.notInclude(summary, "secret");
961+
assert.notInclude(summary, "https://");
962+
assert.notInclude(summary, "Headers:");
963+
}
964+
assert.equal(
965+
DesktopUpdates.describeUpdateErrorCause("404 Not Found\nbody-secret"),
966+
"404 Not Found",
967+
);
968+
});
969+
970+
it("bounds long messages, parallel failures, and cyclic causes", () => {
971+
const longError = new Error("x".repeat(10_000));
972+
const parallel = Cause.combine(Cause.fail(longError), Cause.die(longError));
973+
assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(parallel).length, 1024);
974+
975+
const cyclic = new Error("retry failed");
976+
cyclic.cause = cyclic;
977+
assert.include(DesktopUpdates.describeUpdateErrorCause(cyclic), "[truncated cause]");
978+
assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(cyclic).length, 1024);
979+
});
980+
981+
it("does not serialize arbitrary payloads or let unreadable errors break logging", () => {
982+
assert.equal(DesktopUpdates.describeUpdateErrorCause({ payload: "secret" }), "object");
983+
const unreadable = Object.defineProperty({}, "message", {
984+
get() {
985+
throw new Error("unreadable error");
986+
},
987+
});
988+
assert.equal(DesktopUpdates.describeUpdateErrorCause(unreadable), "UnknownError");
989+
});
990+
});

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type DesktopUpdateCheckResult,
88
type DesktopUpdateState,
99
} from "@t3tools/contracts";
10+
import { errorTag } from "@t3tools/shared/observability";
1011
import * as Cause from "effect/Cause";
1112
import * as Context from "effect/Context";
1213
import * as DateTime from "effect/DateTime";
@@ -34,7 +35,6 @@ import * as IpcChannels from "../ipc/channels.ts";
3435
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
3536
import { normalizeDesktopUpdateReleaseNotes } from "./releaseNotes.ts";
3637
import { resolveDefaultDesktopUpdateChannel } from "./updateChannels.ts";
37-
import { describeUpdateErrorCause } from "./updateDiagnostics.ts";
3838
import {
3939
createInitialDesktopUpdateState,
4040
reduceDesktopUpdateStateOnCheckFailure,
@@ -197,6 +197,63 @@ const {
197197
logError: logUpdaterError,
198198
} = DesktopObservability.makeComponentLogger("desktop-updater");
199199

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+
200257
function parseAppUpdateYml(raw: string): Effect.Effect<Option.Option<AppUpdateYmlConfig>> {
201258
const entries: Record<string, string> = {};
202259
for (const line of raw.split("\n")) {

‎apps/desktop/src/updates/updateDiagnostics.test.ts‎

Lines changed: 0 additions & 62 deletions
This file was deleted.

‎apps/desktop/src/updates/updateDiagnostics.ts‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)