Skip to content

Commit 93361a0

Browse files
Guy BedfordJakeChampion
authored andcommitted
switch error handling polarity, streamline
1 parent d9a789d commit 93361a0

File tree

10 files changed

+237
-283
lines changed

10 files changed

+237
-283
lines changed

c-dependencies/js-compute-runtime/builtins/backend.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -811,13 +811,12 @@ JS::Result<mozilla::Ok> Backend::register_dynamic_backend(JSContext *cx, JS::Han
811811
}
812812

813813
fastly_error_t err;
814-
auto result =
815-
xqd_fastly_http_req_register_dynamic_backend(&name_str, &target_str, &backend_config, &err);
816-
if (!HANDLE_RESULT(cx, result, err)) {
814+
if (!xqd_fastly_http_req_register_dynamic_backend(&name_str, &target_str, &backend_config,
815+
&err)) {
816+
HANDLE_ERROR(cx, err);
817817
return JS::Result<mozilla::Ok>(JS::Error());
818-
} else {
819-
return mozilla::Ok();
820818
}
819+
return mozilla::Ok();
821820
}
822821

823822
JSString *Backend::name(JSContext *cx, JSObject *self) {

c-dependencies/js-compute-runtime/builtins/config-store.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ bool ConfigStore::get(JSContext *cx, unsigned argc, JS::Value *vp) {
3030
fastly_option_string_t ret;
3131
fastly_error_t err;
3232
// Ensure that we throw an exception for all unexpected host errors.
33-
if (!HANDLE_RESULT(
34-
cx,
35-
xqd_fastly_dictionary_get(ConfigStore::config_store_handle(self), &key_str, &ret, &err),
36-
err))
33+
if (!xqd_fastly_dictionary_get(ConfigStore::config_store_handle(self), &key_str, &ret, &err)) {
34+
HANDLE_ERROR(cx, err);
3735
return false;
36+
}
3837

39-
// FastlyStatus::none indicates the key wasn't found, so we return null.
38+
// None indicates the key wasn't found, so we return null.
4039
if (!ret.is_some) {
4140
args.rval().setNull();
4241
return true;
@@ -100,14 +99,16 @@ bool ConfigStore::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
10099
JS::RootedObject config_store(cx, JS_NewObjectForConstructor(cx, &class_, args));
101100
fastly_dictionary_handle_t dict_handle = INVALID_HANDLE;
102101
fastly_error_t err;
103-
bool is_error = xqd_fastly_dictionary_open(&name_str, &dict_handle, &err);
104-
if (is_error && err == FASTLY_ERROR_BAD_HANDLE) {
105-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_DOES_NOT_EXIST,
106-
name.data());
107-
return false;
102+
if (!xqd_fastly_dictionary_open(&name_str, &dict_handle, &err)) {
103+
if (err == FASTLY_ERROR_BAD_HANDLE) {
104+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CONFIG_STORE_DOES_NOT_EXIST,
105+
name.data());
106+
return false;
107+
} else {
108+
HANDLE_ERROR(cx, err);
109+
return false;
110+
}
108111
}
109-
if (!HANDLE_RESULT(cx, is_error, err))
110-
return false;
111112

112113
JS::SetReservedSlot(config_store, ConfigStore::Slots::Handle, JS::Int32Value(dict_handle));
113114
if (!config_store)

c-dependencies/js-compute-runtime/builtins/dictionary.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ bool Dictionary::get(JSContext *cx, unsigned argc, JS::Value *vp) {
3737
fastly_error_t err;
3838

3939
// Ensure that we throw an exception for all unexpected host errors.
40-
if (!HANDLE_RESULT(
41-
cx, xqd_fastly_dictionary_get(Dictionary::dictionary_handle(self), &name_str, &ret, &err),
42-
err))
40+
if (!xqd_fastly_dictionary_get(Dictionary::dictionary_handle(self), &name_str, &ret, &err)) {
41+
HANDLE_ERROR(cx, err);
4342
return false;
43+
}
4444

4545
if (!ret.is_some) {
4646
args.rval().setNull();
@@ -113,14 +113,15 @@ bool Dictionary::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
113113

114114
fastly_dictionary_handle_t dict_handle = INVALID_HANDLE;
115115
fastly_error_t err;
116-
bool is_error = xqd_fastly_dictionary_open(&name_str, &dict_handle, &err);
117-
if (is_error && err == FASTLY_ERROR_BAD_HANDLE) {
118-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_DICTIONARY_DOES_NOT_EXIST,
119-
name_str.ptr);
120-
return false;
121-
}
122-
if (!HANDLE_RESULT(cx, is_error, err)) {
123-
return false;
116+
if (!xqd_fastly_dictionary_open(&name_str, &dict_handle, &err)) {
117+
if (err == FASTLY_ERROR_BAD_HANDLE) {
118+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_DICTIONARY_DOES_NOT_EXIST,
119+
name_str.ptr);
120+
return false;
121+
} else {
122+
HANDLE_ERROR(cx, err);
123+
return false;
124+
}
124125
}
125126

126127
JS::SetReservedSlot(dictionary, Dictionary::Slots::Handle, JS::Int32Value(dict_handle));

c-dependencies/js-compute-runtime/builtins/logger.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ bool Logger::log(JSContext *cx, unsigned argc, JS::Value *vp) {
1616

1717
fastly_error_t err;
1818
xqd_world_string_t msg_str = {msg.get(), msg_len};
19-
if (!HANDLE_RESULT(cx, xqd_fastly_log_write(endpoint, &msg_str, &err), err))
19+
if (!xqd_fastly_log_write(endpoint, &msg_str, &err)) {
20+
HANDLE_ERROR(cx, err);
2021
return false;
22+
}
2123

2224
args.rval().setUndefined();
2325
return true;
@@ -35,8 +37,10 @@ JSObject *Logger::create(JSContext *cx, const char *name) {
3537
fastly_log_endpoint_handle_t handle;
3638
xqd_world_string_t name_str = {const_cast<char *>(name), strlen(name)};
3739
fastly_error_t err;
38-
if (!HANDLE_RESULT(cx, xqd_fastly_log_endpoint_get(&name_str, &handle, &err), err))
40+
if (!xqd_fastly_log_endpoint_get(&name_str, &handle, &err)) {
41+
HANDLE_ERROR(cx, err);
3942
return nullptr;
43+
}
4044

4145
JS::SetReservedSlot(logger, Slots::Endpoint, JS::Int32Value(handle));
4246

@@ -54,7 +58,8 @@ bool Logger::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
5458
fastly_log_endpoint_handle_t handle = INVALID_HANDLE;
5559
fastly_error_t err;
5660

57-
if (!HANDLE_RESULT(cx, xqd_fastly_log_endpoint_get(&name_str, &handle, &err), err)) {
61+
if (!xqd_fastly_log_endpoint_get(&name_str, &handle, &err)) {
62+
HANDLE_ERROR(cx, err);
5863
return false;
5964
}
6065

c-dependencies/js-compute-runtime/builtins/object-store.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ bool get(JSContext *cx, unsigned argc, JS::Value *vp) {
191191

192192
fastly_option_body_handle_t ret;
193193
fastly_error_t err;
194-
if (!HANDLE_RESULT(
195-
cx, xqd_fastly_object_store_lookup(object_store_handle(self), &key_str, &ret, &err), err))
194+
if (!xqd_fastly_object_store_lookup(object_store_handle(self), &key_str, &ret, &err)) {
195+
HANDLE_ERROR(cx, err);
196196
return false;
197+
}
197198

198199
// When no entry is found, we are going to resolve the Promise with `null`.
199200
if (!ret.is_some) {
@@ -264,10 +265,10 @@ bool put(JSContext *cx, unsigned argc, JS::Value *vp) {
264265
fastly_body_handle_t body = RequestOrResponse::body_handle(source_owner);
265266

266267
fastly_error_t err;
267-
if (!HANDLE_RESULT(
268-
cx, xqd_fastly_object_store_insert(object_store_handle(self), &key_str, body, &err),
269-
err))
268+
if (!xqd_fastly_object_store_insert(object_store_handle(self), &key_str, body, &err)) {
269+
HANDLE_ERROR(cx, err);
270270
return ReturnPromiseRejectedWithPendingError(cx, args);
271+
}
271272

272273
// The insert was successful so we return a Promise which resolves to undefined
273274
JS::RootedValue rval(cx);
@@ -320,30 +321,31 @@ bool put(JSContext *cx, unsigned argc, JS::Value *vp) {
320321

321322
fastly_body_handle_t body_handle = INVALID_HANDLE;
322323
fastly_error_t err;
323-
if (!HANDLE_RESULT(cx, xqd_fastly_http_body_new(&body_handle, &err), err)) {
324+
if (!xqd_fastly_http_body_new(&body_handle, &err)) {
325+
HANDLE_ERROR(cx, err);
324326
return ReturnPromiseRejectedWithPendingError(cx, args);
325327
}
326328

327329
if (body_handle == INVALID_HANDLE) {
328330
return ReturnPromiseRejectedWithPendingError(cx, args);
329331
}
330332

331-
bool is_error = write_to_body_all(body_handle, buf, length, &err);
333+
bool ok = write_to_body_all(body_handle, buf, length, &err);
332334

333-
// Ensure that the NoGC is reset, so throwing an error in HANDLE_RESULT
335+
// Ensure that the NoGC is reset, so throwing an error in HANDLE_ERROR
334336
// succeeds.
335337
if (maybeNoGC.isSome()) {
336338
maybeNoGC.reset();
337339
}
338340

339-
if (!HANDLE_RESULT(cx, is_error, err)) {
341+
if (!ok) {
342+
HANDLE_ERROR(cx, err);
340343
return ReturnPromiseRejectedWithPendingError(cx, args);
341344
}
342345

343-
is_error =
344-
xqd_fastly_object_store_insert(object_store_handle(self), &key_str, body_handle, &err);
345-
// Ensure that we throw an exception for all unexpected host errors.
346-
if (!HANDLE_RESULT(cx, is_error, err)) {
346+
if (!xqd_fastly_object_store_insert(object_store_handle(self), &key_str, body_handle, &err)) {
347+
// Ensure that we throw an exception for all unexpected host errors.
348+
HANDLE_ERROR(cx, err);
347349
return RejectPromiseWithPendingError(cx, result_promise);
348350
}
349351

@@ -406,15 +408,15 @@ bool constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
406408

407409
fastly_object_store_handle_t object_store_handle = INVALID_HANDLE;
408410
fastly_error_t err;
409-
bool is_error = xqd_fastly_object_store_open(&name_str, &object_store_handle, &err);
410-
if (is_error && err == FASTLY_ERROR_INVALID_ARGUMENT) {
411-
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_OBJECT_STORE_DOES_NOT_EXIST,
412-
name_str.ptr);
413-
return false;
414-
}
415-
416-
if (!HANDLE_RESULT(cx, is_error, err)) {
417-
return false;
411+
if (!xqd_fastly_object_store_open(&name_str, &object_store_handle, &err)) {
412+
if (err == FASTLY_ERROR_INVALID_ARGUMENT) {
413+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_OBJECT_STORE_DOES_NOT_EXIST,
414+
name_str.ptr);
415+
return false;
416+
} else {
417+
HANDLE_ERROR(cx, err);
418+
return false;
419+
}
418420
}
419421

420422
JS::RootedObject object_store(cx, JS_NewObjectForConstructor(cx, &class_, args));

c-dependencies/js-compute-runtime/geo_ip.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ JSString *get_geo_info(JSContext *cx, JS::HandleString address_str) {
3838

3939
xqd_world_string_t ret;
4040
fastly_error_t err;
41-
if (!HANDLE_RESULT(cx, xqd_fastly_geo_lookup(&octets_list, &ret, &err), err)) {
41+
if (!xqd_fastly_geo_lookup(&octets_list, &ret, &err)) {
42+
HANDLE_ERROR(cx, err);
4243
return nullptr;
4344
}
4445

c-dependencies/js-compute-runtime/host_call.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,6 @@
11

22
#include "host_call.h"
33

4-
FastlyStatus convert_to_fastly_status(bool is_err, fastly_error_t error) {
5-
if (!is_err)
6-
return FastlyStatus::Ok;
7-
switch (error) {
8-
case FASTLY_ERROR_GENERIC_ERROR:
9-
return FastlyStatus::Error;
10-
case FASTLY_ERROR_INVALID_ARGUMENT:
11-
return FastlyStatus::Inval;
12-
case FASTLY_ERROR_BAD_HANDLE:
13-
return FastlyStatus::BadF;
14-
case FASTLY_ERROR_BUFFER_LEN:
15-
return FastlyStatus::BufLen;
16-
case FASTLY_ERROR_UNSUPPORTED:
17-
return FastlyStatus::Unsupported;
18-
case FASTLY_ERROR_BAD_ALIGN:
19-
return FastlyStatus::BadAlign;
20-
case FASTLY_ERROR_HTTP_INVALID:
21-
return FastlyStatus::HttpInvalid;
22-
case FASTLY_ERROR_HTTP_USER:
23-
return FastlyStatus::HttpUser;
24-
case FASTLY_ERROR_HTTP_INCOMPLETE:
25-
return FastlyStatus::HttpIncomplete;
26-
case FASTLY_ERROR_OPTIONAL_NONE:
27-
return FastlyStatus::None;
28-
case FASTLY_ERROR_HTTP_HEAD_TOO_LARGE:
29-
return FastlyStatus::HttpHeadTooLarge;
30-
case FASTLY_ERROR_HTTP_INVALID_STATUS:
31-
return FastlyStatus::HttpInvalidStatus;
32-
case FASTLY_ERROR_LIMIT_EXCEEDED:
33-
return FastlyStatus::LimitExceeded;
34-
default:
35-
MOZ_ASSERT_UNREACHABLE("coding error");
36-
return FastlyStatus::Unknown;
37-
}
38-
}
39-
404
bool OwnedHostCallBuffer::initialize(JSContext *cx) {
415
// Ensure the buffer is all zeros so it doesn't add too much to the
426
// snapshot.

0 commit comments

Comments
 (0)