Skip to content

Commit 5a1a6a6

Browse files
committed
fix(server): keep browser output out of the html preview error message
1 parent 00a9b67 commit 5a1a6a6

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ describe("HtmlRender", () => {
264264
describe.skipIf(HostProcess.Platform.defaultValue() === "win32")(
265265
"when the browser exits before rendering",
266266
() => {
267-
it.live("reports its exit code and the last line it printed", () =>
267+
it.live("reports its exit code and keeps what it printed out of the message", () =>
268268
Effect.gen(function* () {
269269
const executable = yield* exitingBrowser(
270270
`process.stderr.write("[WARNING:bus.cc] Failed to connect to the bus\\n[FATAL:shared_memory.cc] Check failed: /dev/shm is not writable\\n"); process.exit(3);`,
@@ -273,7 +273,7 @@ describe("HtmlRender", () => {
273273
const error = yield* previewError(executable);
274274

275275
expect(error.message).toBe(
276-
"Headless Chrome could not render the page: the browser exited unexpectedly (exit code 3): [FATAL:shared_memory.cc] Check failed: /dev/shm is not writable.",
276+
"Headless Chrome could not render the page: the browser exited unexpectedly (exit code 3).",
277277
);
278278
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
279279
);

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ export class HtmlRenderBrowserError extends Schema.TaggedError<HtmlRenderBrowser
1919
cause: Schema.optional(Schema.Defect()),
2020
/** The end of the browser's stderr when it exited, for diagnosing host setup. */
2121
output: Schema.optional(Schema.String),
22+
exitCode: Schema.optional(Schema.Number),
23+
signal: Schema.optional(Schema.String),
2224
},
2325
) {
2426
override get message(): string {
25-
return `Headless Chrome could not render the page: ${this.reason}.`;
27+
const exit =
28+
this.exitCode !== undefined
29+
? ` (exit code ${this.exitCode})`
30+
: this.signal !== undefined
31+
? ` (signal ${this.signal})`
32+
: "";
33+
return `Headless Chrome could not render the page: ${this.reason}${exit}.`;
2634
}
2735
}
2836

@@ -35,7 +43,6 @@ export interface ConsoleMessage {
3543

3644
const MAX_CONSOLE_MESSAGES = 20;
3745
const MAX_CONSOLE_TEXT_CHARS = 500;
38-
const MAX_EXIT_OUTPUT_CHARS = 300;
3946
const VIEWPORT_HEIGHT = 800;
4047
const MAX_CAPTURE_HEIGHT = 4_000;
4148
const CAPTURE_TIMEOUT = "20 seconds";
@@ -163,17 +170,7 @@ const Navigation = Schema.Struct({ errorText: Schema.optional(Schema.String) });
163170
const Measured = Schema.Struct({ result: Schema.Struct({ value: Schema.Finite }) });
164171

165172
// Effect's spawner names the signal that ended a child only in its error message.
166-
const EXIT_SIGNAL = /signal: '(\w+)'/;
167-
168-
/** Why the browser went away: its exit code or signal, and the last line it printed. */
169-
const exitReason = (status: string | undefined, output: string) => {
170-
const lastLine = output.trimEnd().split("\n").at(-1)?.trim() ?? "";
171-
const said =
172-
lastLine.length > MAX_EXIT_OUTPUT_CHARS
173-
? `${lastLine.slice(0, MAX_EXIT_OUTPUT_CHARS)}…`
174-
: lastLine;
175-
return `the browser exited unexpectedly${status ? ` (${status})` : ""}${said ? `: ${said}` : ""}`;
176-
};
173+
const EXIT_SIGNAL = /signal: '(SIG[A-Z0-9]+)'/;
177174

178175
interface PageEvents {
179176
/** The page's main frame, which only ever shows `PAGE_URL`. */
@@ -348,16 +345,17 @@ const launchBrowser = Effect.fnUntraced(function* (input: {
348345
// sandbox, says why on stderr, which may still be draining, so give it a moment.
349346
const disconnect = Effect.gen(function* () {
350347
yield* Fiber.await(stderrReader).pipe(Effect.timeout("1 second"), Effect.ignore);
351-
const status = yield* child.exitCode.pipe(
352-
Effect.map((code) => `exit code ${code}`),
348+
const exit = yield* child.exitCode.pipe(
349+
Effect.map((code): { exitCode?: number; signal?: string } => ({ exitCode: code })),
353350
Effect.catch((error) =>
354-
Effect.succeed(`signal ${EXIT_SIGNAL.exec(String(error.cause))?.[1] ?? "unknown"}`),
351+
Effect.succeed({ signal: EXIT_SIGNAL.exec(String(error.cause))?.[1] ?? "unknown" }),
355352
),
356353
Effect.timeoutOption("1 second"),
357354
);
358355
const error = new HtmlRenderBrowserError({
359-
reason: exitReason(Option.getOrUndefined(status), stderrTail),
356+
reason: "the browser exited unexpectedly",
360357
output: stderrTail,
358+
...Option.getOrElse(exit, () => ({})),
361359
});
362360
disconnected = error;
363361
const waiters = [...pending.values()];

0 commit comments

Comments
 (0)