Skip to content

Commit 02184fd

Browse files
Jake ChampionJakeChampion
authored andcommitted
Throw an error if deleting a kv store key which does not exist
1 parent 68dfec7 commit 02184fd

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

documentation/docs/fastly:kv-store/KVStore/prototype/delete.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Returns `undefined`
3131
- Starts with the string `".well-known/acme-challenge/"`
3232
- Contains any of the characters `"#?*[]\n\r"`
3333
- Is longer than 1024 characters
34+
- Does not exist
3435

3536
## Examples
3637

integration-tests/js-compute/fixtures/app/src/kv-store.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* globals KVStoreEntry */
22
import { pass, assert, assertThrows, assertRejects, assertResolves } from "./assertions.js";
33
import { KVStore } from "fastly:kv-store";
4-
import { routes } from "./routes.js";
4+
import { routes, isRunningLocally } from "./routes.js";
55
// KVStore
66
{
77
routes.set("/kv-store/exposed-as-global", async () => {
@@ -616,8 +616,11 @@ import { routes } from "./routes.js";
616616
return pass()
617617
});
618618
routes.set("/kv-store/delete/key-does-not-exist-returns-undefined", async () => {
619+
if (isRunningLocally()) {
620+
return pass()
621+
}
619622
let store = createValidStore()
620-
let error = await assertRejects(() => store.delete(Math.random()), "KVStore.prototype.delete: can not delete key which does not exist")
623+
let error = await assertRejects(() => store.delete(Math.random()), TypeError, "KVStore.prototype.delete: can not delete key which does not exist")
621624
if (error) { return error }
622625
return pass()
623626
});
@@ -634,6 +637,9 @@ import { routes } from "./routes.js";
634637
return pass()
635638
});
636639
routes.set("/kv-store/delete/delete-key-twice", async () => {
640+
if (isRunningLocally()) {
641+
return pass()
642+
}
637643
let store = createValidStore()
638644
let key = `key-exists-${Math.random()}`;
639645
await store.put(key, 'hello')
@@ -643,7 +649,7 @@ import { routes } from "./routes.js";
643649
result = await result
644650
error = assert(result, undefined, `(await store.delete(key) === undefined)`)
645651
if (error) { return error }
646-
error = await assertRejects(() => store.delete(key), "KVStore.prototype.delete: can not delete key which does not exist")
652+
error = await assertRejects(() => store.delete(key), TypeError, "KVStore.prototype.delete: can not delete key which does not exist")
647653
if (error) { return error }
648654
return pass()
649655
});

runtime/js-compute-runtime/builtins/kv-store.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,20 @@ bool parse_and_validate_key(JSContext *cx, const char *key, size_t len) {
185185

186186
bool KVStore::process_pending_kv_store_delete(JSContext *cx, int32_t handle,
187187
JS::HandleObject context, JS::HandleObject promise) {
188-
host_api::ObjectStorePendingDelete pending_lookup(handle);
188+
host_api::ObjectStorePendingDelete pending_delete(handle);
189189

190-
auto res = pending_lookup.wait();
190+
auto res = pending_delete.wait();
191191
if (auto *err = res.to_err()) {
192-
HANDLE_ERROR(cx, *err);
192+
if (host_api::error_is_invalid_argument(*err)) {
193+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
194+
JSMSG_KV_STORE_DELETE_KEY_DOES_NOT_EXIST);
195+
} else {
196+
HANDLE_ERROR(cx, *err);
197+
}
193198
return RejectPromiseWithPendingError(cx, promise);
194199
}
195200

196201
JS::ResolvePromise(cx, promise, JS::UndefinedHandleValue);
197-
198202
return true;
199203
}
200204

runtime/js-compute-runtime/error-numbers.msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ MSG_DEF(JSMSG_KV_STORE_NAME_EMPTY, 0, JSEXN_TYPEERR,
6060
MSG_DEF(JSMSG_KV_STORE_NAME_TOO_LONG, 0, JSEXN_TYPEERR, "KVStore constructor: name can not be more than 255 characters")
6161
MSG_DEF(JSMSG_KV_STORE_NAME_NO_CONTROL_CHARACTERS, 0, JSEXN_TYPEERR, "KVStore constructor: name can not contain control characters (\\u0000-\\u001F)")
6262
MSG_DEF(JSMSG_KV_STORE_DOES_NOT_EXIST, 1, JSEXN_TYPEERR, "KVStore constructor: No KVStore named '{0}' exists")
63+
MSG_DEF(JSMSG_KV_STORE_DELETE_KEY_DOES_NOT_EXIST, 0, JSEXN_TYPEERR, "KVStore.prototype.delete: can not delete key which does not exist")
6364
MSG_DEF(JSMSG_KV_STORE_KEY_EMPTY, 0, JSEXN_TYPEERR, "KVStore key can not be an empty string")
6465
MSG_DEF(JSMSG_KV_STORE_KEY_TOO_LONG, 0, JSEXN_TYPEERR, "KVStore key can not be more than 1024 characters")
6566
MSG_DEF(JSMSG_KV_STORE_KEY_INVALID_CHARACTER, 1, JSEXN_TYPEERR, "KVStore key can not contain {0} character")

0 commit comments

Comments
 (0)