From e98ca56c4abe575b88ca76566e85773cf378438b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 08:15:29 +0000 Subject: [PATCH] fix(studio): an in-place tween patch that throws says why it fell back to a reload (#8454) * test(studio): an in-place tween patch that throws reports the error's name * fix(studio): an in-place tween patch that throws says why it fell back to a reload * test(studio): a composition's own error class name reports as other * fix(studio): only built-in error names leave an in-place patch's report * test(studio): a built-in error from the preview frame's realm reports its name * fix(studio): an in-place patch reads a thrown error's name from any realm --- .../studio/src/hooks/gsapRuntimePatch.test.ts | 83 +++++++++++++++++++ packages/studio/src/hooks/gsapRuntimePatch.ts | 28 ++++++- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/packages/studio/src/hooks/gsapRuntimePatch.test.ts b/packages/studio/src/hooks/gsapRuntimePatch.test.ts index e34d293a41..dcb9fc951f 100644 --- a/packages/studio/src/hooks/gsapRuntimePatch.test.ts +++ b/packages/studio/src/hooks/gsapRuntimePatch.test.ts @@ -1,5 +1,9 @@ +import { runInNewContext } from "node:vm"; import { describe, expect, it, vi } from "vitest"; import { patchRuntimeTweenInPlace } from "./gsapRuntimePatch"; +import { trackStudioEvent } from "../utils/studioTelemetry"; + +vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() })); /** * The helper patches ONE tween's values in `window.__timelines[compKey]` in place, @@ -566,3 +570,82 @@ describe("patchRuntimeTweenInPlace — deferSeek", () => { expect(rendered).toEqual({ a: 500, b: 600 }); }); }); + +describe("patchRuntimeTweenInPlace — a throw inside the patch", () => { + // fails without the fix: the reload fallback hid the error, so a broken patch path looked like a slow edit. + // The composition's GSAP throws here, so the edit still falls back to a reload. + it("falls back, and reports the error's name, never its message", () => { + const logged = vi.spyOn(console, "error").mockImplementation(() => {}); + const el = { id: "box" }; + const iframe = { + contentWindow: { + gsap: { + set: () => { + throw new TypeError("user script text"); + }, + }, + }, + contentDocument: { querySelector: (sel: string) => (sel === "#box" ? el : null) }, + } as unknown as HTMLIFrameElement; + + expect(patchRuntimeTweenInPlace(iframe, "#box", { kind: "global-set", props: { x: 1 } })).toBe( + false, + ); + expect(trackStudioEvent).toHaveBeenCalledWith("gsap_instant_patch_threw", { + error_name: "TypeError", + }); + expect(logged).toHaveBeenCalledTimes(1); + logged.mockRestore(); + }); + + // fails without the fix: a TypeError from the preview frame's realm is not instanceof this realm's Error, so every + // real throw reported 'other'. + it("reports a built-in error thrown in the preview frame's realm by its name", () => { + const logged = vi.spyOn(console, "error").mockImplementation(() => {}); + const el = { id: "box" }; + const iframe = { + contentWindow: { + gsap: { + set: () => { + throw runInNewContext("new TypeError('user script text')"); + }, + }, + }, + contentDocument: { querySelector: (sel: string) => (sel === "#box" ? el : null) }, + } as unknown as HTMLIFrameElement; + + expect(patchRuntimeTweenInPlace(iframe, "#box", { kind: "global-set", props: { x: 1 } })).toBe( + false, + ); + expect(trackStudioEvent).toHaveBeenLastCalledWith("gsap_instant_patch_threw", { + error_name: "TypeError", + }); + logged.mockRestore(); + }); + + it("reports a name the composition gave its own error class as 'other'", () => { + const logged = vi.spyOn(console, "error").mockImplementation(() => {}); + class FilmError extends Error { + override name = "user script text"; + } + const el = { id: "box" }; + const iframe = { + contentWindow: { + gsap: { + set: () => { + throw new FilmError(); + }, + }, + }, + contentDocument: { querySelector: (sel: string) => (sel === "#box" ? el : null) }, + } as unknown as HTMLIFrameElement; + + expect(patchRuntimeTweenInPlace(iframe, "#box", { kind: "global-set", props: { x: 1 } })).toBe( + false, + ); + expect(trackStudioEvent).toHaveBeenLastCalledWith("gsap_instant_patch_threw", { + error_name: "other", + }); + logged.mockRestore(); + }); +}); diff --git a/packages/studio/src/hooks/gsapRuntimePatch.ts b/packages/studio/src/hooks/gsapRuntimePatch.ts index 806f931b39..7b7cb3cf89 100644 --- a/packages/studio/src/hooks/gsapRuntimePatch.ts +++ b/packages/studio/src/hooks/gsapRuntimePatch.ts @@ -11,9 +11,31 @@ import { type RuntimeTweenChange, } from "@hyperframes/core/runtime/patch-tween"; import { swapWaiting, type SoftReloadWindow } from "@hyperframes/core/runtime/soft-reload"; +import { trackStudioEvent } from "../utils/studioTelemetry"; export type { RuntimeTweenChange, SetPatchProps } from "@hyperframes/core/runtime/patch-tween"; +// A composition can name its own error class anything, so only these names leave the machine. +const REPORTED_ERROR_NAMES = new Set([ + "Error", + "TypeError", + "RangeError", + "ReferenceError", + "SyntaxError", + "EvalError", + "URIError", + "AggregateError", + "SecurityError", + "InvalidStateError", + "NotFoundError", + "DataCloneError", +]); +// The error may come from the preview frame's realm, so its name is read, never checked with instanceof. +function reportedErrorName(error: unknown): string { + const name = (error as { name?: unknown } | null)?.name; + return typeof name === "string" && REPORTED_ERROR_NAMES.has(name) ? name : "other"; +} + function reachablePreview(iframe: HTMLIFrameElement): PatchTarget { const win = iframe.contentWindow as | (Window & { @@ -48,7 +70,11 @@ export function patchRuntimeTweenInPlace( if (change.kind !== "global-set") return patchTween(target, selector, change, compositionId, deferSeek); return applyGlobalSet(target, selector, change.props) !== null; - } catch { + } catch (error) { + trackStudioEvent("gsap_instant_patch_threw", { + error_name: reportedErrorName(error), + }); + console.error("[Studio] An in-place tween patch threw; reloading the preview instead", error); return false; } }