fix(engine): a page that keeps throwing looks up each error's throw site once, a few in all (#8810)

* fix(engine): a page that keeps throwing looks up each error's throw site once, a few in all

* fix(engine): the throw-site bound adds no comment lines to the capture file
This commit is contained in:
Miguel Ángel
2026-11-25 05:03:13 +00:00
committed by GitHub
parent 14d582c0f5
commit 7d9a14418b
2 changed files with 45 additions and 4 deletions
@@ -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<unknown> = async () => ({}),
) {
const runtimeListeners = new Map<string, (event: unknown) => void>();
const pageListeners = new Map<string, (event: unknown) => 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`;
+8 -2
View File
@@ -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<string>;
/** 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<void>
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;