Skip to content

Commit

Permalink
Don't throw REST errors when 'try' is set to true
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jun 21, 2024
1 parent 8af0f65 commit 0493d66
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
16 changes: 10 additions & 6 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25400,7 +25400,8 @@ bool flecs_rest_script(
flecs_rest_bool_param(req, "try", &try);

bool prev_color = ecs_log_enable_colors(false);
rest_prev_log = ecs_os_api.log_;
ecs_os_api_log_t prev_log = ecs_os_api.log_;
rest_prev_log = try ? NULL : prev_log;
ecs_os_api.log_ = flecs_rest_capture_log;

script = ecs_script(world, {
Expand All @@ -25423,7 +25424,7 @@ bool flecs_rest_script(
ecs_os_free(err);
}

ecs_os_api.log_ = rest_prev_log;
ecs_os_api.log_ = prev_log;
ecs_log_enable_colors(prev_color);

return true;
Expand Down Expand Up @@ -25556,7 +25557,7 @@ bool flecs_rest_get_query(

const char *expr = ecs_http_get_param(req, "expr");
if (!expr) {
ecs_strbuf_appendlit(&reply->body, "Missing parameter 'q'");
ecs_strbuf_appendlit(&reply->body, "Missing parameter 'expr'");
reply->code = 400; /* bad request */
return true;
}
Expand All @@ -25566,7 +25567,8 @@ bool flecs_rest_get_query(

ecs_dbg_2("rest: request query '%s'", expr);
bool prev_color = ecs_log_enable_colors(false);
rest_prev_log = ecs_os_api.log_;
ecs_os_api_log_t prev_log = ecs_os_api.log_;
rest_prev_log = try ? NULL : prev_log;
ecs_os_api.log_ = flecs_rest_capture_log;

ecs_query_t *q = ecs_query(world, { .expr = expr });
Expand All @@ -25582,7 +25584,7 @@ bool flecs_rest_get_query(
ecs_query_fini(q);
}

ecs_os_api.log_ = rest_prev_log;
ecs_os_api.log_ = prev_log;
ecs_log_enable_colors(prev_color);

return true;
Expand Down Expand Up @@ -43480,7 +43482,7 @@ bool flecs_json_serialize_iter_result_ids(

ecs_world_t *world = it->world;
int16_t f, field_count = flecs_ito(int16_t, it->field_count);
int16_t field_mask = flecs_ito(int16_t, (1u << field_count) - 1);
int16_t field_mask = flecs_ito(int16_t, (1 << field_count) - 1);
if (q->static_id_fields == field_mask) {
/* All matched ids are static, nothing to serialize */
return false;
Expand Down Expand Up @@ -43587,6 +43589,7 @@ bool flecs_json_serialize_common_for_table(
return result;
}

static
int flecs_json_serialize_iter_result_field_values(
const ecs_world_t *world,
const ecs_iter_t *it,
Expand Down Expand Up @@ -43696,6 +43699,7 @@ int flecs_json_serialize_iter_result_query(
if (flecs_json_serialize_iter_result_field_values(
world, it, i, buf, desc, ser_ctx))
{
ecs_os_free(common_data);
return -1;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/addons/json/serialize_iter_result_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool flecs_json_serialize_iter_result_ids(

ecs_world_t *world = it->world;
int16_t f, field_count = flecs_ito(int16_t, it->field_count);
int16_t field_mask = flecs_ito(int16_t, (1u << field_count) - 1);
int16_t field_mask = flecs_ito(int16_t, (1 << field_count) - 1);
if (q->static_id_fields == field_mask) {
/* All matched ids are static, nothing to serialize */
return false;
Expand Down Expand Up @@ -153,6 +153,7 @@ bool flecs_json_serialize_common_for_table(
return result;
}

static
int flecs_json_serialize_iter_result_field_values(
const ecs_world_t *world,
const ecs_iter_t *it,
Expand Down Expand Up @@ -262,6 +263,7 @@ int flecs_json_serialize_iter_result_query(
if (flecs_json_serialize_iter_result_field_values(
world, it, i, buf, desc, ser_ctx))
{
ecs_os_free(common_data);
return -1;
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/addons/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ bool flecs_rest_script(
flecs_rest_bool_param(req, "try", &try);

bool prev_color = ecs_log_enable_colors(false);
rest_prev_log = ecs_os_api.log_;
ecs_os_api_log_t prev_log = ecs_os_api.log_;
rest_prev_log = try ? NULL : prev_log;
ecs_os_api.log_ = flecs_rest_capture_log;

script = ecs_script(world, {
Expand All @@ -529,7 +530,7 @@ bool flecs_rest_script(
ecs_os_free(err);
}

ecs_os_api.log_ = rest_prev_log;
ecs_os_api.log_ = prev_log;
ecs_log_enable_colors(prev_color);

return true;
Expand Down Expand Up @@ -662,7 +663,7 @@ bool flecs_rest_get_query(

const char *expr = ecs_http_get_param(req, "expr");
if (!expr) {
ecs_strbuf_appendlit(&reply->body, "Missing parameter 'q'");
ecs_strbuf_appendlit(&reply->body, "Missing parameter 'expr'");
reply->code = 400; /* bad request */
return true;
}
Expand All @@ -672,7 +673,8 @@ bool flecs_rest_get_query(

ecs_dbg_2("rest: request query '%s'", expr);
bool prev_color = ecs_log_enable_colors(false);
rest_prev_log = ecs_os_api.log_;
ecs_os_api_log_t prev_log = ecs_os_api.log_;
rest_prev_log = try ? NULL : prev_log;
ecs_os_api.log_ = flecs_rest_capture_log;

ecs_query_t *q = ecs_query(world, { .expr = expr });
Expand All @@ -688,7 +690,7 @@ bool flecs_rest_get_query(
ecs_query_fini(q);
}

ecs_os_api.log_ = rest_prev_log;
ecs_os_api.log_ = prev_log;
ecs_log_enable_colors(prev_color);

return true;
Expand Down
10 changes: 5 additions & 5 deletions test/addons/src/Rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ void Rest_try_query(void) {
{
ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT;
test_int(-1, ecs_http_server_request(srv, "GET",
"/query?q=Foo", &reply));
"/query?expr=Foo", &reply));
test_int(reply.code, 400); // No try, should error
ecs_strbuf_reset(&reply.body);
}

{
ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT;
test_int(0, ecs_http_server_request(srv, "GET",
"/query?q=Foo&try=true", &reply));
"/query?expr=Foo&try=true", &reply));
test_int(reply.code, 200); // With try, should not error
ecs_strbuf_reset(&reply.body);
}
Expand All @@ -160,13 +160,13 @@ void Rest_query(void) {

ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT;
test_int(0, ecs_http_server_request(srv, "GET",
"/query?q=Position", &reply));
"/query?expr=Position", &reply));
test_int(reply.code, 200);

char *reply_str = ecs_strbuf_get(&reply.body);
test_assert(reply_str != NULL);
test_str(reply_str,
"{\"results\":[{\"name\":\"e\", \"fields\":[{}]}]}");
"{\"results\":[{\"name\":\"e\", \"fields\":{\"values\":[0]}}]}");
ecs_os_free(reply_str);

ecs_rest_server_fini(srv);
Expand Down Expand Up @@ -200,7 +200,7 @@ void Rest_named_query(void) {
char *reply_str = ecs_strbuf_get(&reply.body);
test_assert(reply_str != NULL);
test_str(reply_str,
"{\"results\":[{\"name\":\"e\", \"fields\":[{}]}]}");
"{\"results\":[{\"name\":\"e\", \"fields\":{\"values\":[0]}}]}");
ecs_os_free(reply_str);

ecs_rest_server_fini(srv);
Expand Down

0 comments on commit 0493d66

Please sign in to comment.