Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(core): Response error code for invalid result
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Mar 15, 2019
1 parent fb6c29c commit cb3d926
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions accelerator/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ void set_options_method_header(served::response& res) {
res.set_header("Access-Control-Max-Age", "86400");
}

status_t set_response_content(status_t ret, char** json_result) {
status_t http_ret;
if (ret == SC_OK) {
return SC_HTTP_OK;
}

cJSON* json_obj = cJSON_CreateObject();
if (ret == SC_CCLIENT_NOT_FOUND) {
http_ret = SC_NOT_FOUND;
cJSON_AddStringToObject(json_obj, "message", "Request not found");
} else if (ret == SC_SERIALIZER_JSON_PARSE) {
http_ret = SC_BAD_REQUEST;
cJSON_AddStringToObject(json_obj, "message", "Invalid request header");
} else {
http_ret = SC_INTERNAL_SERVICE_ERROR;
cJSON_AddStringToObject(json_obj, "message", "Internal service error");
}
*json_result = cJSON_PrintUnformatted(json_obj);
return http_ret;
}

int main(int, char const**) {
served::multiplexer mux;
mux.use_after(served::plugin::access_log);
Expand Down Expand Up @@ -62,12 +83,15 @@ int main(int, char const**) {
set_options_method_header(res);
})
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
&json_result);
ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
&json_result);
ret = set_response_content(ret, &json_result);
res.set_header("Content-Type", "application/json");
res.set_header("Access-Control-Allow-Origin", "*");
res.set_status(ret);
res << json_result;
});

Expand All @@ -84,12 +108,15 @@ int main(int, char const**) {
set_options_method_header(res);
})
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

api_get_transaction_object(&service, req.params["tx"].c_str(),
&json_result);
ret = api_get_transaction_object(&service, req.params["tx"].c_str(),
&json_result);
ret = set_response_content(ret, &json_result);
res.set_header("Content-Type", "application/json");
res.set_header("Access-Control-Allow-Origin", "*");
res.set_status(ret);
res << json_result;
});

Expand All @@ -106,12 +133,15 @@ int main(int, char const**) {
set_options_method_header(res);
})
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

api_find_transactions_obj_by_tag(&service, req.params["tag"].c_str(),
&json_result);
ret = api_find_transactions_obj_by_tag(
&service, req.params["tag"].c_str(), &json_result);
ret = set_response_content(ret, &json_result);
res.set_header("Content-Type", "application/json");
res.set_header("Access-Control-Allow-Origin", "*");
res.set_status(ret);
res << json_result;
});

Expand All @@ -126,11 +156,14 @@ int main(int, char const**) {
set_options_method_header(res);
})
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

api_get_tips_pair(&service, &json_result);
ret = api_get_tips_pair(&service, &json_result);
ret = set_response_content(ret, &json_result);
res.set_header("Content-Type", "application/json");
res.set_header("Access-Control-Allow-Origin", "*");
res.set_status(ret);
res << json_result;
});

Expand All @@ -145,11 +178,14 @@ int main(int, char const**) {
set_options_method_header(res);
})
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

api_get_tips(&service, &json_result);
ret = api_get_tips(&service, &json_result);
ret = set_response_content(ret, &json_result);
res.set_header("Content-Type", "application/json");
res.set_header("Access-Control-Allow-Origin", "*");
res.set_status(ret);
res << json_result;
});

Expand All @@ -164,11 +200,14 @@ int main(int, char const**) {
set_options_method_header(res);
})
.get([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

api_generate_address(&service, &json_result);
ret = api_generate_address(&service, &json_result);
ret = set_response_content(ret, &json_result);
res.set_header("Content-Type", "application/json");
res.set_header("Access-Control-Allow-Origin", "*");
res.set_status(ret);
res << json_result;
});

Expand All @@ -183,6 +222,7 @@ int main(int, char const**) {
set_options_method_header(res);
})
.post([&](served::response& res, const served::request& req) {
status_t ret = SC_OK;
char* json_result;

if (req.header("content-type").find("application/json") ==
Expand All @@ -195,7 +235,9 @@ int main(int, char const**) {
res.set_status(SC_BAD_REQUEST);
cJSON_Delete(json_obj);
} else {
api_send_transfer(&service, req.body().c_str(), &json_result);
ret = api_send_transfer(&service, req.body().c_str(), &json_result);
ret = set_response_content(ret, &json_result);
res.set_status(ret);
}

res.set_header("Content-Type", "application/json");
Expand Down

0 comments on commit cb3d926

Please sign in to comment.