From 8a23ee530cd1d7d7b4e93e9e72f4e06d1fc3d845 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Mon, 11 Mar 2024 12:32:20 -0700 Subject: [PATCH] add handling for success=false responses (#10387) --- .changeset/soft-foxes-grin.md | 5 +++++ packages/db/src/core/cli/commands/push/index.ts | 15 ++++++++++++++- packages/db/src/core/cli/migration-queries.ts | 14 +++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 .changeset/soft-foxes-grin.md diff --git a/.changeset/soft-foxes-grin.md b/.changeset/soft-foxes-grin.md new file mode 100644 index 000000000000..d3992e19b2ad --- /dev/null +++ b/.changeset/soft-foxes-grin.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +handle success=false response on api endpoints diff --git a/packages/db/src/core/cli/commands/push/index.ts b/packages/db/src/core/cli/commands/push/index.ts index 990ca05b9198..738eb877f250 100644 --- a/packages/db/src/core/cli/commands/push/index.ts +++ b/packages/db/src/core/cli/commands/push/index.ts @@ -82,11 +82,24 @@ async function pushSchema({ return new Response(null, { status: 200 }); } const url = new URL('/db/push', getRemoteDatabaseUrl()); - return await fetch(url, { + const response = await fetch(url, { method: 'POST', headers: new Headers({ Authorization: `Bearer ${appToken}`, }), body: JSON.stringify(requestBody), }); + if (!response.ok) { + console.error(`${url.toString()} failed: ${response.status} ${response.statusText}`); + console.error(await response.text()); + throw new Error(`/db/push fetch failed: ${response.status} ${response.statusText}`); + } + const result = (await response.json()) as + | { success: false; } + | { success: true; }; + if (!result.success) { + console.error(`${url.toString()} unsuccessful`); + console.error(await response.text()); + throw new Error(`/db/push fetch unsuccessful`); + } } diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts index 1cd9345acaaf..3ff80d009e7b 100644 --- a/packages/db/src/core/cli/migration-queries.ts +++ b/packages/db/src/core/cli/migration-queries.ts @@ -432,7 +432,19 @@ export async function getProductionCurrentSnapshot({ Authorization: `Bearer ${appToken}`, }), }); - const result = await response.json(); + if (!response.ok) { + console.error(`${url.toString()} failed: ${response.status} ${response.statusText}`); + console.error(await response.text()); + throw new Error(`/db/schema fetch failed: ${response.status} ${response.statusText}`); + } + const result = (await response.json()) as + | { success: false; data: undefined } + | { success: true; data: DBSnapshot }; + if (!result.success) { + console.error(`${url.toString()} unsuccessful`); + console.error(await response.text()); + throw new Error(`/db/schema fetch unsuccessful`); + } return result.data; }