Skip to content

Commit

Permalink
add handling for success=false responses (#10387)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Mar 11, 2024
1 parent d461eb7 commit 8a23ee5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-foxes-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

handle success=false response on api endpoints
15 changes: 14 additions & 1 deletion packages/db/src/core/cli/commands/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
}
14 changes: 13 additions & 1 deletion packages/db/src/core/cli/migration-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 8a23ee5

Please sign in to comment.