From 2efb55ecb5e5f5df02192b1de53facbb26cd90e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 18 Nov 2026 12:39:41 +0000 Subject: [PATCH] fix(studio): refused group, freeze frame and animation saves keep the server's status (#8178) * fix(studio): refused group, freeze frame and animation saves keep the server's status * refactor(studio): withStatus says what it does without a comment --- .../src/hooks/gsapMutationClient.test.ts | 32 +++++++++++++++++++ .../studio/src/hooks/gsapMutationClient.ts | 9 ++++-- .../src/hooks/timelineTimingSync.test.ts | 2 +- .../studio/src/hooks/useFreezeFrame.test.ts | 5 +-- packages/studio/src/hooks/useFreezeFrame.ts | 4 +-- packages/studio/src/hooks/useGroupCommits.ts | 9 ++++-- .../studio/src/utils/studioSaveDiagnostics.ts | 2 ++ 7 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 packages/studio/src/hooks/gsapMutationClient.test.ts diff --git a/packages/studio/src/hooks/gsapMutationClient.test.ts b/packages/studio/src/hooks/gsapMutationClient.test.ts new file mode 100644 index 0000000000..7c9a62fd32 --- /dev/null +++ b/packages/studio/src/hooks/gsapMutationClient.test.ts @@ -0,0 +1,32 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { GsapPreviewConvergenceError, postGsapMutation } from "./gsapMutationClient"; + +afterEach(() => vi.unstubAllGlobals()); + +const refuse = (status: number, body: unknown) => + vi.stubGlobal( + "fetch", + vi.fn(async () => new Response(JSON.stringify(body), { status })), + ); + +describe("postGsapMutation", () => { + // fails without the fix: a refused mutation threw the server's words without its status. + it("a refused mutation keeps the server's words and its status", async () => { + refuse(400, { error: "No tween at that index" }); + const refused = postGsapMutation( + "p", + "index.html", + { op: "update" }, + "Couldn't save animation", + ); + await expect(refused).rejects.toBeInstanceOf(GsapPreviewConvergenceError); + await expect(refused).rejects.toThrow("No tween at that index (400)"); + }); + + it("a refusal with no words of its own keeps the fallback and the status", async () => { + refuse(500, {}); + await expect( + postGsapMutation("p", "index.html", { op: "update" }, "Couldn't save animation"), + ).rejects.toThrow("Couldn't save animation (500)"); + }); +}); diff --git a/packages/studio/src/hooks/gsapMutationClient.ts b/packages/studio/src/hooks/gsapMutationClient.ts index bd3564e426..ea12b89cfd 100644 --- a/packages/studio/src/hooks/gsapMutationClient.ts +++ b/packages/studio/src/hooks/gsapMutationClient.ts @@ -1,6 +1,6 @@ import { studioWriteHeaders } from "../utils/studioFileVersion"; import { studioApiFetch } from "../utils/studioApiFetch"; -import { heldFileWordsOf } from "../utils/studioSaveDiagnostics"; +import { heldFileWordsOf, withStatus } from "../utils/studioSaveDiagnostics"; export class GsapPreviewConvergenceError extends Error {} export class GsapOwnershipProtocolError extends GsapPreviewConvergenceError {} @@ -43,7 +43,8 @@ export async function rollbackOwnedMutation( ); if (!response.ok) { throw new Error( - (await heldFileWordsOf(response, targetPath)) ?? `Failed to restore ${targetPath}`, + (await heldFileWordsOf(response, targetPath)) ?? + withStatus(`Failed to restore ${targetPath}`, response), ); } const result = (await response.json()) as { restored?: unknown; conflict?: unknown }; @@ -127,7 +128,9 @@ async function postOwnedGsapMutation( const held = response.ok ? null : await heldFileWordsOf(response, filePath); const payload: unknown = await response.json().catch(() => null); if (!response.ok) { - throw new GsapPreviewConvergenceError(held ?? readMutationError(payload, fallback)); + throw new GsapPreviewConvergenceError( + held ?? withStatus(readMutationError(payload, fallback), response), + ); } return readMutationStatus(payload); } diff --git a/packages/studio/src/hooks/timelineTimingSync.test.ts b/packages/studio/src/hooks/timelineTimingSync.test.ts index 7cb94add67..85974a1b49 100644 --- a/packages/studio/src/hooks/timelineTimingSync.test.ts +++ b/packages/studio/src/hooks/timelineTimingSync.test.ts @@ -778,7 +778,7 @@ describe("foldGsapMutationIntoHistory — owned GSAP transaction", () => { expect(reloadPreview).toHaveBeenCalledTimes(1); expect(consoleError).toHaveBeenCalledWith( expect.stringContaining("GSAP"), - expect.objectContaining({ message: "Failed to restore index.html" }), + expect.objectContaining({ message: "Failed to restore index.html (503)" }), ); }); diff --git a/packages/studio/src/hooks/useFreezeFrame.test.ts b/packages/studio/src/hooks/useFreezeFrame.test.ts index c6e779a7c3..b2d92b5e4d 100644 --- a/packages/studio/src/hooks/useFreezeFrame.test.ts +++ b/packages/studio/src/hooks/useFreezeFrame.test.ts @@ -51,11 +51,12 @@ describe("requestFreezeFrame", () => { }); }); - it("surfaces the server's error", async () => { + // fails without the fix: the server's words came through without its status, so the failure could not be named. + it("surfaces the server's error with its status", async () => { stubFetch({ status: 400, body: { error: "Move the playhead inside a video clip to freeze" } }); await expect( requestFreezeFrame({ projectId: "p", path: "index.html", element, playhead: 7 }), - ).rejects.toThrow("Move the playhead inside a video clip to freeze"); + ).rejects.toThrow("Move the playhead inside a video clip to freeze (400)"); }); it("names the file and why when the file is read-only", async () => { diff --git a/packages/studio/src/hooks/useFreezeFrame.ts b/packages/studio/src/hooks/useFreezeFrame.ts index 0f74beb4cb..03bf588b29 100644 --- a/packages/studio/src/hooks/useFreezeFrame.ts +++ b/packages/studio/src/hooks/useFreezeFrame.ts @@ -7,7 +7,7 @@ import { markStudioWriteToken } from "../utils/studioFileVersion"; import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator"; import type { RecordEditInput } from "../utils/studioFileHistory"; import { studioApiFetch } from "../utils/studioApiFetch"; -import { heldFileWordsOf } from "../utils/studioSaveDiagnostics"; +import { heldFileWordsOf, withStatus } from "../utils/studioSaveDiagnostics"; type ProjectFileWriter = (path: string, content: string, expectedContent?: string) => Promise; @@ -77,7 +77,7 @@ export async function requestFreezeFrame(input: { const held = response.ok ? null : await heldFileWordsOf(response, input.path); const body: unknown = await response.json().catch(() => null); if (!response.ok || !isFreezeFrameResponse(body)) { - throw new Error(held ?? errorOf(body) ?? `Freeze frame failed (${response.status})`); + throw new Error(held ?? withStatus(errorOf(body) ?? "Freeze frame failed", response)); } return body; } diff --git a/packages/studio/src/hooks/useGroupCommits.ts b/packages/studio/src/hooks/useGroupCommits.ts index 0a520ba4cf..f35bd891cc 100644 --- a/packages/studio/src/hooks/useGroupCommits.ts +++ b/packages/studio/src/hooks/useGroupCommits.ts @@ -17,7 +17,7 @@ import { } from "../utils/studioHelpers"; import type { TimelineElement } from "../player"; import { studioApiFetch } from "../utils/studioApiFetch"; -import { heldFileWordsOf } from "../utils/studioSaveDiagnostics"; +import { heldFileWordsOf, withStatus } from "../utils/studioSaveDiagnostics"; interface UseGroupCommitsParams extends DomEditCommitBaseParams { /** Resync the SDK session after a server-side write (the wrapper/unwrap changes @@ -148,7 +148,12 @@ async function commitStructuralMutation( const errBody = (await mutateResponse.json().catch(() => null)) as { error?: string; } | null; - throw new Error(errBody?.error ?? `Failed to ${label.toLowerCase()} in ${targetPath}`); + throw new Error( + withStatus( + errBody?.error ?? `Failed to ${label.toLowerCase()} in ${targetPath}`, + mutateResponse, + ), + ); } result = (await mutateResponse.json()) as { content?: string; groupId?: string }; return { disk: typeof result.content === "string" ? result.content : originalContent }; diff --git a/packages/studio/src/utils/studioSaveDiagnostics.ts b/packages/studio/src/utils/studioSaveDiagnostics.ts index 27019a6bab..bf0d4086fe 100644 --- a/packages/studio/src/utils/studioSaveDiagnostics.ts +++ b/packages/studio/src/utils/studioSaveDiagnostics.ts @@ -262,6 +262,8 @@ function heldFileMessage(body: string, path: string | undefined): string | null } } +export const withStatus = (words: string, response: Response) => `${words} (${response.status})`; + export async function heldFileWordsOf(response: Response, path: string): Promise { return heldFileMessage( await response