Skip to content

Commit 579b242

Browse files
committed
fix: Improve error messages for missing Blob token in BlobCheck, BlobCreate, BlobSync, and BlobUpdate functions
1 parent 461bd63 commit 579b242

4 files changed

Lines changed: 15 additions & 11 deletions

File tree

data/controls/blobs/blobCheck.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { VetResult } from "@/app/types/vet-result";
22
import { list } from "@vercel/blob";
33

4-
export default async function BlobCheck(blobKey: string, data: VetResult[] = [], options?: { token?: string }) {
5-
const token = options?.token ?? process.env.BLOB_READ_WRITE_TOKEN;
6-
if (!token) throw new Error("Missing Blob token");
4+
export default async function BlobCheck(blobKey: string, data: VetResult[] = []) {
5+
const token = process.env.BLOB_READ_WRITE_TOKEN;
6+
77
try {
8-
const historicBlobs = await list({ token });
8+
const historicBlobs = await list();
99
return historicBlobs.blobs.some((blob) => blob.pathname === blobKey);
1010

1111
} catch (error) {
1212
console.warn(`⚠️ Failed to list blobs for ${blobKey}:`, error);
13+
if (!token) throw new Error("Missing Blob token, failed to Check");
1314
return false;
1415

1516
}

data/controls/blobs/blobCreate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { put } from "@vercel/blob";
22

33
export default async function BlobCreate(blobKey: string, options?: { token?: string }) {
44
const token = options?.token ?? process.env.BLOB_READ_WRITE_TOKEN;
5-
if (!token) throw new Error("Missing Blob token");
5+
if (!token) throw new Error("Missing Blob token, failed to Create");
66
await put(blobKey, JSON.stringify([], null, 2), {
77
access: "public",
88
contentType: "application/json",

data/controls/blobs/blobSync.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { VetResult } from "@/app/types/vet-result";
22
import BlobCheck from "./blobCheck";
33
import BlobCreate from "./blobCreate";
44
import BlobUpdate from "./blobUpdate";
5+
const token = process.env.BLOB_READ_WRITE_TOKEN;
56

67
export default async function BlobSync(region: string, results: VetResult[]) {
78
try {
@@ -16,21 +17,23 @@ export default async function BlobSync(region: string, results: VetResult[]) {
1617
};
1718

1819

19-
const exists = await BlobCheck(blobKey);
20+
const exists = await BlobCheck(blobKey, results);
2021
if (!exists) {
2122
console.log("⚠️ Blob not found, creating...");
2223
// ☁️ Upload to Vercel Blob
23-
await BlobCreate(blobKey);
24-
}
25-
else if (results.length > 0) {
24+
await BlobCreate(blobKey, { token });
25+
} else if (exists && results.length > 0) {
2626
console.log("✅ Blob already exists")
2727
console.log("⚠️ Updating existing blob with new results...");
2828

29-
const blob = await BlobUpdate(blobKey, payload);
29+
const blob = await BlobUpdate(blobKey, payload, { token });
3030

3131
console.log(`🚀 Uploaded to Blob: ${blob.url}`);
3232
console.log(`📥 Download URL: ${blob.downloadUrl}`);
3333
}
34+
else if (exists && results.length === 0) {
35+
console.log("ℹ️ Blob exists, but no new results to update.");
36+
}
3437
console.log("🔍 BlobCheck result:", exists);
3538

3639

data/controls/blobs/blobUpdate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default async function BlobUpdate(blobKey: string, payload: Payload = {
1212
results: []
1313
}, options?: { token?: string }) {
1414
const token = options?.token ?? process.env.BLOB_READ_WRITE_TOKEN;
15-
if (!token) throw new Error("Missing Blob token");
15+
if (!token) throw new Error("Missing Blob token, failed to Update");
1616
const blob = await put(blobKey, JSON.stringify(payload, null, 2), {
1717
access: "public",
1818
contentType: "application/json",

0 commit comments

Comments
 (0)