Skip to content

Commit f9fd9df

Browse files
Update error message for KV to be shorter (#7996)
1 parent cf09cfa commit f9fd9df

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

.changeset/thin-countries-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": patch
3+
---
4+
5+
Provide shorter message for KV GET errors.

packages/workers-shared/asset-worker/src/utils/kv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export async function getAssetWithMetadataFromKV(
4343
return asset;
4444
} catch (err) {
4545
if (attempts >= retries) {
46-
let message = `Requested asset ${assetKey} could not be fetched from KV namespace.`;
46+
let message = `KV GET ${assetKey} failed.`;
4747
if (err instanceof Error) {
48-
message = `Requested asset ${assetKey} could not be fetched from KV namespace: ${err.message}`;
48+
message = `KV GET ${assetKey} failed: ${err.message}`;
4949
}
5050
throw new Error(message);
5151
}

packages/workers-shared/asset-worker/tests/kv.test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,15 @@ describe("[Asset Worker] Fetching assets from KV", () => {
4545

4646
await expect(() =>
4747
getAssetWithMetadataFromKV(mockKVNamespace, "abcd")
48-
).rejects.toThrowError(
49-
"Requested asset abcd could not be fetched from KV namespace."
50-
);
48+
).rejects.toThrowError("KV GET abcd failed.");
5149
});
5250

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

5654
await expect(() =>
5755
getAssetWithMetadataFromKV(mockKVNamespace, "abcd")
58-
).rejects.toThrowError(
59-
"Requested asset abcd could not be fetched from KV namespace."
60-
);
56+
).rejects.toThrowError("KV GET abcd failed.");
6157
expect(spy).toHaveBeenCalledTimes(2);
6258
});
6359

@@ -66,12 +62,21 @@ describe("[Asset Worker] Fetching assets from KV", () => {
6662

6763
await expect(() =>
6864
getAssetWithMetadataFromKV(mockKVNamespace, "abcd", undefined, 2)
69-
).rejects.toThrowError(
70-
"Requested asset abcd could not be fetched from KV namespace."
71-
);
65+
).rejects.toThrowError("KV GET abcd failed.");
7266
expect(spy).toHaveBeenCalledTimes(3);
7367
});
7468

69+
it("should inject message with error", async () => {
70+
spy.mockReturnValue(
71+
Promise.reject(new Error("Oeps! Something went wrong"))
72+
);
73+
74+
await expect(() =>
75+
getAssetWithMetadataFromKV(mockKVNamespace, "abcd")
76+
).rejects.toThrowError("KV GET abcd failed: Oeps! Something went wrong");
77+
expect(spy).toHaveBeenCalledTimes(2);
78+
});
79+
7580
it("should retry on 404 and cache with 30s ttl", async () => {
7681
let attempts = 0;
7782
spy.mockImplementation(() => {

0 commit comments

Comments
 (0)