diff --git a/apps/desktop/src/ipc/methods/window.test.ts b/apps/desktop/src/ipc/methods/window.test.ts new file mode 100644 index 0000000000..609d3e30a1 --- /dev/null +++ b/apps/desktop/src/ipc/methods/window.test.ts @@ -0,0 +1,34 @@ +import { assert, describe, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; + +import { openExternal } from "./window.ts"; +import * as ElectronShell from "../../electron/ElectronShell.ts"; + +describe("window IPC", () => { + it.effect("returns false when Electron rejects an external URL", () => { + const url = "https://example.com/path"; + const error = new ElectronShell.ElectronShellOpenExternalError({ + urlHostname: "example.com", + urlLength: url.length, + urlProtocol: "https:", + cause: new Error("open failed"), + }); + const layer = Layer.mergeAll( + Layer.succeed( + ElectronShell.ElectronShell, + ElectronShell.ElectronShell.of({ + openExternal: () => Effect.fail(error), + copyText: () => Effect.void, + }), + ), + Logger.layer([], { mergeWithExisting: false }), + ); + + return Effect.gen(function* () { + const opened = yield* openExternal.handler(url); + assert.equal(opened, false); + }).pipe(Effect.provide(layer)); + }); +}); diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index 3cb705d036..b638b77e6a 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -7,6 +7,7 @@ import { } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts"; @@ -141,6 +142,19 @@ export const openExternal = DesktopIpc.makeIpcMethod({ result: Schema.Boolean, handler: Effect.fn("desktop.ipc.window.openExternal")(function* (url) { const shell = yield* ElectronShell.ElectronShell; - return yield* shell.openExternal(url); + return yield* shell.openExternal(url).pipe( + Effect.catchTag("ElectronShellOpenExternalError", (error) => + Effect.logWarning(error.message).pipe( + Effect.annotateLogs({ + error: Redacted.make(error, { label: error._tag }), + "error.type": error._tag, + "desktop.external_url.hostname": error.urlHostname, + "desktop.external_url.input_length": error.urlLength, + "desktop.external_url.protocol": error.urlProtocol, + }), + Effect.as(false), + ), + ), + ); }), }); diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 0d703cf397..fbdbf5a34f 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -942,6 +942,7 @@ export interface DesktopBridge { items: readonly ContextMenuItem[], position?: { x: number; y: number }, ) => Promise; + /** Resolves false when the URL is unsafe or the desktop shell cannot open it. */ openExternal: (url: string) => Promise; onMenuAction: (listener: (action: string) => void) => () => void; getUpdateState: () => Promise;