Skip to content

Commit 761be94

Browse files
juliusmarmingecodex
andcommitted
fix(desktop): preserve external-open IPC result
Co-authored-by: codex <[email protected]>
1 parent 8fd276f commit 761be94

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import * as Logger from "effect/Logger";
5+
6+
import { openExternal } from "./window.ts";
7+
import * as ElectronShell from "../../electron/ElectronShell.ts";
8+
9+
describe("window IPC", () => {
10+
it.effect("returns false when Electron rejects an external URL", () => {
11+
const url = "https://example.com/path";
12+
const error = new ElectronShell.ElectronShellOpenExternalError({
13+
urlHostname: "example.com",
14+
urlLength: url.length,
15+
urlProtocol: "https:",
16+
cause: new Error("open failed"),
17+
});
18+
const layer = Layer.mergeAll(
19+
Layer.succeed(
20+
ElectronShell.ElectronShell,
21+
ElectronShell.ElectronShell.of({
22+
openExternal: () => Effect.fail(error),
23+
copyText: () => Effect.void,
24+
}),
25+
),
26+
Logger.layer([], { mergeWithExisting: false }),
27+
);
28+
29+
return Effect.gen(function* () {
30+
const opened = yield* openExternal.handler(url);
31+
assert.equal(opened, false);
32+
}).pipe(Effect.provide(layer));
33+
});
34+
});

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@t3tools/contracts";
88
import * as Effect from "effect/Effect";
99
import * as Option from "effect/Option";
10+
import * as Redacted from "effect/Redacted";
1011
import * as Schema from "effect/Schema";
1112

1213
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
@@ -141,6 +142,19 @@ export const openExternal = DesktopIpc.makeIpcMethod({
141142
result: Schema.Boolean,
142143
handler: Effect.fn("desktop.ipc.window.openExternal")(function* (url) {
143144
const shell = yield* ElectronShell.ElectronShell;
144-
return yield* shell.openExternal(url);
145+
return yield* shell.openExternal(url).pipe(
146+
Effect.catchTag("ElectronShellOpenExternalError", (error) =>
147+
Effect.logWarning(error.message).pipe(
148+
Effect.annotateLogs({
149+
error: Redacted.make(error, { label: error._tag }),
150+
"error.type": error._tag,
151+
"desktop.external_url.hostname": error.urlHostname,
152+
"desktop.external_url.input_length": error.urlLength,
153+
"desktop.external_url.protocol": error.urlProtocol,
154+
}),
155+
Effect.as(false),
156+
),
157+
),
158+
);
145159
}),
146160
});

‎packages/contracts/src/ipc.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ export interface DesktopBridge {
942942
items: readonly ContextMenuItem<T>[],
943943
position?: { x: number; y: number },
944944
) => Promise<T | null>;
945+
/** Resolves false when the URL is unsafe or the desktop shell cannot open it. */
945946
openExternal: (url: string) => Promise<boolean>;
946947
onMenuAction: (listener: (action: string) => void) => () => void;
947948
getUpdateState: () => Promise<DesktopUpdateState>;

0 commit comments

Comments
 (0)