mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
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
This commit is contained in:
@@ -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)");
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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)" }),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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<void>;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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<string | null> {
|
||||
return heldFileMessage(
|
||||
await response
|
||||
|
||||
Reference in New Issue
Block a user