Skip to content

Commit

Permalink
Update error message for KV to be shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
WillTaylorDev committed Jan 31, 2025
1 parent cf09cfa commit 21ed366
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-countries-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/workers-shared": patch
---

Provide shorter message for KV GET errors.
4 changes: 2 additions & 2 deletions packages/workers-shared/asset-worker/src/utils/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export async function getAssetWithMetadataFromKV(
return asset;
} catch (err) {
if (attempts >= retries) {
let message = `Requested asset ${assetKey} could not be fetched from KV namespace.`;
let message = `KV GET ${assetKey} failed.`;
if (err instanceof Error) {
message = `Requested asset ${assetKey} could not be fetched from KV namespace: ${err.message}`;
message = `KV GET ${assetKey} failed: ${err.message}`;
}
throw new Error(message);
}
Expand Down
12 changes: 3 additions & 9 deletions packages/workers-shared/asset-worker/tests/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,15 @@ describe("[Asset Worker] Fetching assets from KV", () => {

await expect(() =>
getAssetWithMetadataFromKV(mockKVNamespace, "abcd")
).rejects.toThrowError(
"Requested asset abcd could not be fetched from KV namespace."
);
).rejects.toThrowError("KV GET abcd failed.");
});

it("should retry once by default if something went wrong while fetching the asset", async () => {
spy.mockReturnValue(Promise.reject("Oeps! Something went wrong"));

await expect(() =>
getAssetWithMetadataFromKV(mockKVNamespace, "abcd")
).rejects.toThrowError(
"Requested asset abcd could not be fetched from KV namespace."
);
).rejects.toThrowError("KV GET abcd failed.");
expect(spy).toHaveBeenCalledTimes(2);
});

Expand All @@ -66,9 +62,7 @@ describe("[Asset Worker] Fetching assets from KV", () => {

await expect(() =>
getAssetWithMetadataFromKV(mockKVNamespace, "abcd", undefined, 2)
).rejects.toThrowError(
"Requested asset abcd could not be fetched from KV namespace."
);
).rejects.toThrowError("KV GET abcd failed.");
expect(spy).toHaveBeenCalledTimes(3);
});

Expand Down

0 comments on commit 21ed366

Please # to comment.