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

Commit cb3d926

Browse files
committed
feat(core): Response error code for invalid result
1 parent fb6c29c commit cb3d926

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

accelerator/server.cc

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ void set_options_method_header(served::response& res) {
1414
res.set_header("Access-Control-Max-Age", "86400");
1515
}
1616

17+
status_t set_response_content(status_t ret, char** json_result) {
18+
status_t http_ret;
19+
if (ret == SC_OK) {
20+
return SC_HTTP_OK;
21+
}
22+
23+
cJSON* json_obj = cJSON_CreateObject();
24+
if (ret == SC_CCLIENT_NOT_FOUND) {
25+
http_ret = SC_NOT_FOUND;
26+
cJSON_AddStringToObject(json_obj, "message", "Request not found");
27+
} else if (ret == SC_SERIALIZER_JSON_PARSE) {
28+
http_ret = SC_BAD_REQUEST;
29+
cJSON_AddStringToObject(json_obj, "message", "Invalid request header");
30+
} else {
31+
http_ret = SC_INTERNAL_SERVICE_ERROR;
32+
cJSON_AddStringToObject(json_obj, "message", "Internal service error");
33+
}
34+
*json_result = cJSON_PrintUnformatted(json_obj);
35+
return http_ret;
36+
}
37+
1738
int main(int, char const**) {
1839
served::multiplexer mux;
1940
mux.use_after(served::plugin::access_log);
@@ -62,12 +83,15 @@ int main(int, char const**) {
6283
set_options_method_header(res);
6384
})
6485
.get([&](served::response& res, const served::request& req) {
86+
status_t ret = SC_OK;
6587
char* json_result;
6688

67-
api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
68-
&json_result);
89+
ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
90+
&json_result);
91+
ret = set_response_content(ret, &json_result);
6992
res.set_header("Content-Type", "application/json");
7093
res.set_header("Access-Control-Allow-Origin", "*");
94+
res.set_status(ret);
7195
res << json_result;
7296
});
7397

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

89-
api_get_transaction_object(&service, req.params["tx"].c_str(),
90-
&json_result);
114+
ret = api_get_transaction_object(&service, req.params["tx"].c_str(),
115+
&json_result);
116+
ret = set_response_content(ret, &json_result);
91117
res.set_header("Content-Type", "application/json");
92118
res.set_header("Access-Control-Allow-Origin", "*");
119+
res.set_status(ret);
93120
res << json_result;
94121
});
95122

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

111-
api_find_transactions_obj_by_tag(&service, req.params["tag"].c_str(),
112-
&json_result);
139+
ret = api_find_transactions_obj_by_tag(
140+
&service, req.params["tag"].c_str(), &json_result);
141+
ret = set_response_content(ret, &json_result);
113142
res.set_header("Content-Type", "application/json");
114143
res.set_header("Access-Control-Allow-Origin", "*");
144+
res.set_status(ret);
115145
res << json_result;
116146
});
117147

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

131-
api_get_tips_pair(&service, &json_result);
162+
ret = api_get_tips_pair(&service, &json_result);
163+
ret = set_response_content(ret, &json_result);
132164
res.set_header("Content-Type", "application/json");
133165
res.set_header("Access-Control-Allow-Origin", "*");
166+
res.set_status(ret);
134167
res << json_result;
135168
});
136169

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

150-
api_get_tips(&service, &json_result);
184+
ret = api_get_tips(&service, &json_result);
185+
ret = set_response_content(ret, &json_result);
151186
res.set_header("Content-Type", "application/json");
152187
res.set_header("Access-Control-Allow-Origin", "*");
188+
res.set_status(ret);
153189
res << json_result;
154190
});
155191

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

169-
api_generate_address(&service, &json_result);
206+
ret = api_generate_address(&service, &json_result);
207+
ret = set_response_content(ret, &json_result);
170208
res.set_header("Content-Type", "application/json");
171209
res.set_header("Access-Control-Allow-Origin", "*");
210+
res.set_status(ret);
172211
res << json_result;
173212
});
174213

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

188228
if (req.header("content-type").find("application/json") ==
@@ -195,7 +235,9 @@ int main(int, char const**) {
195235
res.set_status(SC_BAD_REQUEST);
196236
cJSON_Delete(json_obj);
197237
} else {
198-
api_send_transfer(&service, req.body().c_str(), &json_result);
238+
ret = api_send_transfer(&service, req.body().c_str(), &json_result);
239+
ret = set_response_content(ret, &json_result);
240+
res.set_status(ret);
199241
}
200242

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

0 commit comments

Comments
 (0)