Skip to content

Commit 135f4de

Browse files
Report a failed Google Doc update instead of an empty success. (#165)
Creating the document and then failing to write its content still returned 200 with success true, so the app opened a blank file. A failed batch update now returns 502 with the document link and a fixed error. Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Gáborjáni-Szabó Ádám <Programcode@users.noreply.github.com>
1 parent b8b41c9 commit 135f4de

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎supabase/functions/export-gdoc/auth_test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,60 @@ Deno.test("export-gdoc lets a valid token through to request validation", async
4343
}]);
4444
});
4545

46+
Deno.test("export-gdoc reports a failed content update instead of an empty success", async () => {
47+
await runBearerCases(handleRequest, (authorization) => new Request("http://127.0.0.1/functions/v1/export-gdoc", {
48+
method: "POST",
49+
headers: { Authorization: authorization!, "Content-Type": "application/json" },
50+
body: JSON.stringify({ mode: "brainstorm", data: { topFive: [{ rank: 1, idea: { title: "Buses" } }] }, accessToken: "test-google-token" }),
51+
}), [{
52+
label: "batch update failed",
53+
authorization: `Bearer ${VALID_TOKEN}`,
54+
status: 502,
55+
body: {
56+
success: false,
57+
error: "The Google Doc was created, but its content could not be saved. Try the export again.",
58+
code: "DOCUMENT_UPDATE_FAILED",
59+
documentUrl: "https://docs.google.com/document/d/doc-1/edit",
60+
documentId: "doc-1",
61+
},
62+
authCalls: 1,
63+
}], (url) => {
64+
if (url.href === "https://docs.googleapis.com/v1/documents") {
65+
return Response.json({ documentId: "doc-1" });
66+
}
67+
if (url.href === "https://docs.googleapis.com/v1/documents/doc-1:batchUpdate") {
68+
return Response.json({ error: { message: "PRIVATE_GOOGLE_BODY" } }, { status: 500 });
69+
}
70+
throw new Error(`Unexpected request: ${url}`);
71+
});
72+
});
73+
74+
Deno.test("export-gdoc returns the document when Google accepts the content", async () => {
75+
await runBearerCases(handleRequest, (authorization) => new Request("http://127.0.0.1/functions/v1/export-gdoc", {
76+
method: "POST",
77+
headers: { Authorization: authorization!, "Content-Type": "application/json" },
78+
body: JSON.stringify({ mode: "brainstorm", data: {}, accessToken: "test-google-token" }),
79+
}), [{
80+
label: "batch update succeeded",
81+
authorization: `Bearer ${VALID_TOKEN}`,
82+
status: 200,
83+
body: {
84+
success: true,
85+
documentUrl: "https://docs.google.com/document/d/doc-2/edit",
86+
documentId: "doc-2",
87+
},
88+
authCalls: 1,
89+
}], (url) => {
90+
if (url.href === "https://docs.googleapis.com/v1/documents") {
91+
return Response.json({ documentId: "doc-2" });
92+
}
93+
if (url.href === "https://docs.googleapis.com/v1/documents/doc-2:batchUpdate") {
94+
return Response.json({ documentId: "doc-2" });
95+
}
96+
throw new Error(`Unexpected request: ${url}`);
97+
});
98+
});
99+
46100
Deno.test("export-gdoc does not allow arbitrary LAN origins", async () => {
47101
const response = await handleRequest(new Request("http://127.0.0.1/functions/v1/export-gdoc", {
48102
method: "OPTIONS", headers: { Origin: "http://192.168.1.5:8080" },

‎supabase/functions/export-gdoc/index.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,18 @@ export async function handleRequest(req: Request): Promise<Response> {
911911
if (!batchUpdateResponse.ok) {
912912
const errorText = await batchUpdateResponse.text();
913913
console.error("[GDOC] Failed to update document:", errorText.slice(0, 2000));
914+
// The empty document already exists in the student's Drive.
915+
// Reporting success opened that blank file as if the export worked.
916+
return new Response(JSON.stringify({
917+
success: false,
918+
error: "The Google Doc was created, but its content could not be saved. Try the export again.",
919+
code: "DOCUMENT_UPDATE_FAILED",
920+
documentUrl,
921+
documentId,
922+
}), {
923+
status: 502,
924+
headers: { ...corsHeaders, "Content-Type": "application/json" }
925+
});
914926
}
915927
}
916928

0 commit comments

Comments
 (0)