diff --git a/packages/engine/src/services/frameCapture.test.ts b/packages/engine/src/services/frameCapture.test.ts index f3cc3b4c27..b14c88ba5c 100644 --- a/packages/engine/src/services/frameCapture.test.ts +++ b/packages/engine/src/services/frameCapture.test.ts @@ -165,13 +165,16 @@ describe("classifyPageError", () => { }); // initializeSession registers its listeners before the incomplete fake session makes it throw. - async function listenedSession(memo: SubTimelineWaitMemo = {}) { + async function listenedSession( + memo: SubTimelineWaitMemo = {}, + send: (method: string) => Promise = async () => ({}), + ) { const runtimeListeners = new Map void>(); const pageListeners = new Map void>(); const listen = (listeners: typeof pageListeners) => (event: string, listener: (event: unknown) => void) => listeners.set(event, listener); - const client = { on: listen(runtimeListeners), send: async () => ({}) }; + const client = { on: listen(runtimeListeners), send }; const page = { on: listen(pageListeners), createCDPSession: async () => client }; const session = { page, @@ -273,6 +276,38 @@ describe("classifyPageError", () => { } }); + // fails without the fix: every throw turned the debugger on and downloaded the whole script again + it("looks up a throw site once per distinct error, for a few errors in all, however often the page throws", async () => { + const sent: string[] = []; + const scriptSource = `${"// filler\n".repeat(1_000_000)}document.getElementById("hero").textContent = 1;`; + const { session, runtimeListeners } = await listenedSession({}, async (method) => { + sent.push(method); + return method === "Debugger.getScriptSource" ? { scriptSource } : {}; + }); + const frame = { url: named, scriptId: "7", lineNumber: 1_000_000, columnNumber: 9 }; + const throwAt = (message: string) => + runtimeListeners.get("Runtime.exceptionThrown")?.({ + exceptionDetails: { + ...thrown(`TypeError: ${message}`, named, []), + stackTrace: { callFrames: [frame as Protocol.Runtime.CallFrame] }, + }, + }); + for (let n = 0; n < 1_000; n++) throwAt("hero is null"); + for (let n = 0; n < 1_000; n++) throwAt(`step ${n} is null`); + + expect(sent.filter((method) => method === "Debugger.enable")).toHaveLength(10); + await vi.waitFor(() => + expect(sent.filter((method) => method === "Debugger.disable")).toHaveLength(10), + ); + expect(sent.filter((method) => method === "Debugger.getScriptSource")).toHaveLength(10); + const hero = session.pageErrors.filter((error) => error.includes("hero is null")); + expect(hero).toEqual([ + `runtime-error:TypeError: hero is null [thrown at: ${named}:1000001:10, getElementById("hero")]`, + ]); + expect(session.pageErrors.filter((error) => error.includes("[thrown at:"))).toHaveLength(10); + expect(session.pageErrors).toHaveLength(50); + }); + it("classifies a script by its whole URL before keeping a cut copy", async () => { const { session, pageListeners } = await listenedSession(); const url = `https://${"a".repeat(2_100)}.example.com/x.js`; diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts index 01996df0e5..e9610ff6e4 100644 --- a/packages/engine/src/services/frameCapture.ts +++ b/packages/engine/src/services/frameCapture.ts @@ -241,6 +241,7 @@ export interface CaptureSession { scriptLoadCauses?: ScriptLoadCause[]; /** Uncaught page errors; a timed-out timeline wait with any of these becomes a script failure. */ pageErrors: string[]; + throwSiteLookups?: Set; /** True once this session honoured `options.strictVariables`: the check ran and passed. */ strictVariablesChecked?: boolean; /** The first VFX chain error; its frames would lack the effect, so it stops the render. */ @@ -596,6 +597,7 @@ const CAPTURE_SESSION_CLOSE_TIMEOUT_MS = 5_000; const PAGE_TEXT_MAX_CHARS = 2_000; const PAGE_LIST_MAX = 50; +const THROW_SITE_LOOKUP_MAX = 10; const CUT_NOTE_MAX_CHARS = 40; @@ -3229,10 +3231,14 @@ export async function initializeSession(session: CaptureSession): Promise const runtimeClient = await getCdpSession(page); runtimeClient.on("Runtime.exceptionThrown", ({ exceptionDetails }) => { const error = classifyPageError(exceptionDetails, projectScripts); - recordPageError(session, error); if (!error) return; - // Recorded first so fail-fast sees it; the throw site is appended once the source is fetched. const kept = boundedPageText(error); + const lookedUp = (session.throwSiteLookups ??= new Set()); + if (lookedUp.has(kept)) return; + // Recorded first so fail-fast sees it; the throw site is appended once the source is fetched. + recordPageError(session, error); + if (lookedUp.size >= THROW_SITE_LOOKUP_MAX || !session.pageErrors.includes(kept)) return; + lookedUp.add(kept); void describeThrowSite(runtimeClient, exceptionDetails, projectScripts).then((site) => { const i = session.pageErrors.indexOf(kept); if (!site || i < 0) return;