Skip to content

Commit 0838f1b

Browse files
authored
KV: improve error messages for 400 errors (#8860)
* KV: improve error messages for 400 errors * update changelog --------- Co-authored-by: talves <[email protected]>
1 parent 234afae commit 0838f1b

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

.changeset/tender-bushes-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
KV: update error messages for 400 errors

packages/miniflare/src/workers/kv/namespace.worker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,17 @@ export class KVNamespaceObject extends MiniflareDurableObject {
136136
const keys: string[] = parsedBody.keys;
137137
const type = parsedBody?.type;
138138
if (type && type !== "text" && type !== "json") {
139-
return new Response("Bad Request", { status: 400 });
139+
const errorStr = `"${type}" is not a valid type. Use "json" or "text"`;
140+
return new Response(errorStr, { status: 400, statusText: errorStr });
140141
}
141142
const obj: { [key: string]: any } = {};
142143
if (keys.length > MAX_BULK_GET_KEYS) {
143-
return new Response("Bad Request", {
144-
status: 400,
145-
});
144+
const errorStr = `You can request a maximum of ${MAX_BULK_GET_KEYS} keys`;
145+
return new Response(errorStr, { status: 400, statusText: errorStr });
146+
}
147+
if (keys.length < 1) {
148+
const errorStr = "You must request a minimum of 1 key";
149+
return new Response(errorStr, { status: 400, statusText: errorStr });
146150
}
147151
let totalBytes = 0;
148152
for (const key of keys) {

packages/miniflare/test/plugins/kv/index.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,34 @@ test("bulk get: check max keys", async (t) => {
146146
try {
147147
await kv.get(keyArray);
148148
} catch (error: any) {
149-
t.is(error.message, "KV GET_BULK failed: 400 Bad Request");
149+
t.is(
150+
error.message,
151+
"KV GET_BULK failed: 400 You can request a maximum of 100 keys"
152+
);
153+
}
154+
});
155+
156+
test("bulk get: check minimum keys", async (t) => {
157+
const { kv } = t.context;
158+
try {
159+
await kv.get([]);
160+
} catch (error: any) {
161+
t.is(
162+
error.message,
163+
"KV GET_BULK failed: 400 You must request a minimum of 1 key"
164+
);
165+
}
166+
});
167+
168+
test("bulk get: invalid type", async (t) => {
169+
const { kv } = t.context;
170+
try {
171+
await kv.get(["key"], { type: "invalid" as "json" });
172+
} catch (error: any) {
173+
t.is(
174+
error.message,
175+
'KV GET_BULK failed: 400 "invalid" is not a valid type. Use "json" or "text"'
176+
);
150177
}
151178
});
152179

0 commit comments

Comments
 (0)