Skip to content

Commit 95885d8

Browse files
author
Guy Bedford
authored
fix: KVStore error handling (#1060)
1 parent 3a77536 commit 95885d8

File tree

19 files changed

+116
-61
lines changed

19 files changed

+116
-61
lines changed

integration-tests/js-compute/fixtures/module-mode/src/kv-store.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@ import {
88
} from './assertions.js';
99
import { KVStore } from 'fastly:kv-store';
1010
import { routes, isRunningLocally } from './routes.js';
11+
import { sdkVersion } from 'fastly:experimental';
12+
13+
const debug = sdkVersion.endsWith('-debug');
1114

1215
// kvstore e2e tests
1316
{
17+
routes.set('/kv-store/debug-error', async () => {
18+
if (!debug) return;
19+
20+
// special debug function to create a kv entry with an invalid handle
21+
const entry = fastly.dump(null, 'invalidkv');
22+
23+
// we can then test the invalid handle error
24+
try {
25+
await entry.text();
26+
} catch (e) {
27+
strictEqual(e.message.includes('Invalid handle'), true);
28+
}
29+
});
30+
1431
routes.set('/kv-store-e2e/list', async () => {
1532
const store = new KVStore('example-test-kv-store');
1633
try {

integration-tests/js-compute/fixtures/module-mode/tests.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"body": "hello world!"
148148
}
149149
},
150+
"GET /kv-store/debug-error": {},
150151
"GET /kv-store/delete/called-as-constructor": {},
151152
"GET /kv-store/delete/called-unbound": {},
152153
"GET /kv-store/delete/key-parameter-calls-7.1.17-ToString": {},

runtime/fastly/builtins/backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#include "fastly.h"
2929

3030
using builtins::BuiltinImpl;
31+
using fastly::FastlyGetErrorMessage;
3132
using fastly::common::parse_and_validate_timeout;
3233
using fastly::fastly::Fastly;
33-
using fastly::fastly::FastlyGetErrorMessage;
3434
using fastly::fetch::RequestOrResponse;
3535
using fastly::secret_store::SecretStoreEntry;
3636

runtime/fastly/builtins/body.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "js/Stream.h"
1616

1717
using builtins::web::streams::NativeStreamSource;
18+
using fastly::FastlyGetErrorMessage;
1819
using fastly::fastly::convertBodyInit;
19-
using fastly::fastly::FastlyGetErrorMessage;
2020
using fastly::fetch::RequestOrResponse;
2121

2222
namespace fastly::body {

runtime/fastly/builtins/cache-override.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "host_api.h"
1414

1515
using builtins::BuiltinImpl;
16-
using fastly::fastly::FastlyGetErrorMessage;
16+
using fastly::FastlyGetErrorMessage;
1717

1818
namespace fastly::cache_override {
1919

runtime/fastly/builtins/cache-simple.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
using builtins::BuiltinNoConstructor;
1515
using builtins::web::streams::NativeStreamSource;
16+
using fastly::FastlyGetErrorMessage;
1617
using fastly::fastly::convertBodyInit;
17-
using fastly::fastly::FastlyGetErrorMessage;
1818
using fastly::fetch::RequestOrResponse;
1919

2020
namespace fastly::cache_simple {

runtime/fastly/builtins/config-store.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "fastly.h"
55

66
using builtins::BuiltinImpl;
7-
using fastly::fastly::FastlyGetErrorMessage;
7+
using fastly::FastlyGetErrorMessage;
88

99
namespace fastly::config_store {
1010

runtime/fastly/builtins/dictionary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "../host-api/host_api_fastly.h"
44
#include "fastly.h"
55

6-
using fastly::fastly::FastlyGetErrorMessage;
6+
using fastly::FastlyGetErrorMessage;
77

88
namespace fastly::dictionary {
99

runtime/fastly/builtins/fastly.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "fastly.h"
1212
#include "js/Conversions.h"
1313
#include "js/JSON.h"
14+
#include "kv-store.h"
1415
#include "logger.h"
1516
#include <arpa/inet.h>
1617

@@ -38,14 +39,6 @@ bool debug_logging_enabled() { return DEBUG_LOGGING_ENABLED; }
3839

3940
namespace fastly::fastly {
4041

41-
const JSErrorFormatString *FastlyGetErrorMessage(void *userRef, unsigned errorNumber) {
42-
if (errorNumber > 0 && errorNumber < JSErrNum_Limit) {
43-
return &fastly_ErrorFormatString[errorNumber];
44-
}
45-
46-
return nullptr;
47-
}
48-
4942
JS::PersistentRooted<JSObject *> Fastly::env;
5043
JS::PersistentRooted<JSObject *> Fastly::baseURL;
5144
JS::PersistentRooted<JSString *> Fastly::defaultBackend;
@@ -57,6 +50,26 @@ bool Fastly::dump(JSContext *cx, unsigned argc, JS::Value *vp) {
5750
if (!args.requireAtLeast(cx, __func__, 1))
5851
return false;
5952

53+
// special debug mode operations
54+
#ifndef NDEBUG
55+
if (args.get(0).isNull() && args.get(1).isString()) {
56+
JS::RootedString str(cx, args.get(1).toString());
57+
auto str_chars = core::encode(cx, str);
58+
if (!str_chars) {
59+
return false;
60+
}
61+
if (!strcmp(str_chars.ptr.get(), "invalidkv")) {
62+
host_api::HttpBody body(-1);
63+
host_api::HostBytes metadata{};
64+
// uint32_t gen = std::get<2>(res.unwrap());
65+
JS::RootedObject entry(
66+
cx, ::fastly::kv_store::KVStoreEntry::create(cx, body, std::move(metadata)));
67+
args.rval().setObject(*entry);
68+
return true;
69+
}
70+
}
71+
#endif
72+
6073
ENGINE->dump_value(args[0], stdout);
6174

6275
args.rval().setUndefined();

runtime/fastly/builtins/fastly.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class Env : public builtins::BuiltinNoConstructor<Env> {
2626
static JSObject *create(JSContext *cx);
2727
};
2828

29-
const JSErrorFormatString *FastlyGetErrorMessage(void *userRef, unsigned errorNumber);
30-
3129
class Fastly : public builtins::BuiltinNoConstructor<Fastly> {
3230
private:
3331
static bool log(JSContext *cx, unsigned argc, JS::Value *vp);

0 commit comments

Comments
 (0)