Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] Add success=false handling to API entrypoints #10387

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading