Skip to content

Commit 60dc0a0

Browse files
t3dotggScottN-PVclaude
authored
fix(server): HTML preview reports a browser start error instead of an internal error (pingdotgg#22895)
Co-authored-by: ScottN-PV <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9c6523d commit 60dc0a0

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎apps/server/src/htmlRender/HtmlRender.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// @effect-diagnostics nodeBuiltinImport:off - plain local servers stand in for LAN services.
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3-
import { describe, expect, it } from "@effect/vitest";
3+
import { assert, describe, expect, it } from "@effect/vitest";
44
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
55
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
66
import * as HostProcess from "@t3tools/shared/HostProcess";
77
import * as Deferred from "effect/Deferred";
88
import * as Effect from "effect/Effect";
9+
import * as Exit from "effect/Exit";
910
import * as Base64 from "effect/encoding/Base64";
1011
import * as Fiber from "effect/Fiber";
1112
import * as FileSystem from "effect/FileSystem";
@@ -19,6 +20,7 @@ import * as NodeURL from "node:url";
1920
import { resolveAttachmentPathById } from "../attachmentStore.ts";
2021
import * as ServerConfig from "../config.ts";
2122
import * as HtmlRender from "./HtmlRender.ts";
23+
import { HtmlRenderBrowserError } from "./headlessChrome.ts";
2224
import * as PreviewBrowser from "../preview/PreviewBrowser.ts";
2325

2426
const TEST_BROWSER_ENV = "T3CODE_TEST_HEADLESS_SHELL";
@@ -287,6 +289,33 @@ describe("HtmlRender", () => {
287289
}),
288290
);
289291

292+
// Windows reports ENOENT asynchronously for this path instead of throwing ENOTDIR.
293+
describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
294+
"when the browser spawn throws synchronously",
295+
() => {
296+
it.live("reports a typed preview error", () =>
297+
Effect.gen(function* () {
298+
const fileSystem = yield* FileSystem.FileSystem;
299+
const path = yield* Path.Path;
300+
const file = yield* fileSystem.makeTempFileScoped({ prefix: "t3-browser-spawn-" });
301+
const outcome = yield* HtmlRender.HtmlRender.use((htmlRender) =>
302+
htmlRender.preview({ html: "<p>x</p>" }).pipe(Effect.flip),
303+
).pipe(
304+
Effect.provide(layerHtmlRender(path.join(file, "chrome-headless-shell"))),
305+
Effect.exit,
306+
);
307+
assert(Exit.isSuccess(outcome), "Expected a typed preview error.");
308+
const error = outcome.value;
309+
expect(error).toBeInstanceOf(HtmlRenderBrowserError);
310+
expect(error.message).toBe(
311+
"Headless Chrome could not render the page: the browser could not be started.",
312+
);
313+
expect(error).toMatchObject({ cause: { code: "ENOTDIR" } });
314+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
315+
);
316+
},
317+
);
318+
290319
it.live("keeps every local file but the page itself out of the browser", (ctx) =>
291320
Effect.gen(function* () {
292321
const executable = (yield* HostProcess.Environment)[TEST_BROWSER_ENV];

‎apps/server/src/htmlRender/headlessChrome.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
229229
(cause) =>
230230
new HtmlRenderBrowserError({ reason: "the browser could not be started", cause }),
231231
),
232+
// Node throws some spawn errors synchronously, such as `spawn UNKNOWN`
233+
// when policy blocks the executable; `mapError` cannot catch those.
234+
Effect.catchDefect((cause) =>
235+
Effect.fail(
236+
new HtmlRenderBrowserError({ reason: "the browser could not be started", cause }),
237+
),
238+
),
232239
);
233240

234241
let stderrTail = "";

0 commit comments

Comments
 (0)