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

Commit cec1168

Browse files
authored
Merge pull request #104 from jkrvivian/server_not_found
Response error code to client when request failed
2 parents 0e95a45 + 6357bba commit cec1168

File tree

3 files changed

+86
-39
lines changed

3 files changed

+86
-39
lines changed

accelerator/apis.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
184184
char* payload = calloc(payload_size * 2 + 1, sizeof(char));
185185

186186
trytes_to_ascii(payload_trytes, payload_size, payload);
187+
if (payload == NULL) {
188+
ret = SC_MAM_NOT_FOUND;
189+
goto done;
190+
}
187191
*json_result = payload;
188192

189193
payload = NULL;

accelerator/errors.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ extern "C" {
2424
#define SC_SEVERITY_MASK 0x18
2525
#define SC_SEVERITY_SHIFT 3
2626

27-
#define SC_SEVERITY_MINOR (0x0 << SC_SEVERITY_MASK)
28-
#define SC_SEVERITY_MODERATE (0x01 << SC_SEVERITY_MASK)
29-
#define SC_SEVERITY_MAJOR (0x02 << SC_SEVERITY_MASK)
30-
#define SC_SEVERITY_FATAL (0x03 << SC_SEVERITY_MASK)
27+
#define SC_SEVERITY_MINOR (0x0 << SC_SEVERITY_SHIFT)
28+
#define SC_SEVERITY_MODERATE (0x01 << SC_SEVERITY_SHIFT)
29+
#define SC_SEVERITY_MAJOR (0x02 << SC_SEVERITY_SHIFT)
30+
#define SC_SEVERITY_FATAL (0x03 << SC_SEVERITY_SHIFT)
3131
/** @} */
3232

3333
/** @name module code */
@@ -47,16 +47,16 @@ extern "C" {
4747
#define SC_ERROR_MASK 0x07
4848
/** @} */
4949

50-
/** @name http error code */
51-
/** @{ */
52-
#define SC_BAD_REQUEST 400
53-
#define SC_NOT_FOUND 404
54-
/** @} */
55-
5650
/* status code */
5751
typedef enum {
5852
SC_OK = 0, /**< No error */
5953

54+
SC_HTTP_OK = 200, /**< HTTP response OK */
55+
SC_HTTP_BAD_REQUEST = 400, /**< HTTP response, error when parsing request */
56+
SC_HTTP_NOT_FOUND = 404, /**< HTTP request not found */
57+
SC_HTTP_INTERNAL_SERVICE_ERROR = 500,
58+
/**< HTTP response, other errors in TA */
59+
6060
SC_TA_OOM = 0x01 | SC_MODULE_TA | SC_SEVERITY_FATAL,
6161
/**< Fail to create TA object */
6262
SC_TA_NULL = 0x02 | SC_MODULE_TA | SC_SEVERITY_FATAL,
@@ -65,35 +65,37 @@ typedef enum {
6565
// CClient module
6666
SC_CCLIENT_OOM = 0x01 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL,
6767
/**< Fail to create cclient object */
68-
SC_CCLIENT_NOT_FOUND = 0x02 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL,
68+
SC_CCLIENT_NOT_FOUND = 0x03 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL,
6969
/**< Empty result from cclient */
70-
SC_CCLIENT_FAILED_RESPONSE = 0x03 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL,
70+
SC_CCLIENT_FAILED_RESPONSE = 0x04 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL,
7171
/**< Error in cclient response */
72-
SC_CCLIENT_INVALID_FLEX_TRITS = 0x04 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
72+
SC_CCLIENT_INVALID_FLEX_TRITS = 0x05 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
7373
/**< flex_trits conversion error */
74-
SC_CCLIENT_HASH = 0x05 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
74+
SC_CCLIENT_HASH = 0x06 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR,
7575
/**< hash container operation error */
7676

7777
// Serializer module
7878
SC_SERIALIZER_JSON_CREATE = 0x01 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,
7979
/**< Fail to create JSON object in serializer */
80-
SC_SERIALIZER_JSON_PARSE = 0x02 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,
81-
/**< Fail to parse JSON object in serializer */
82-
SC_SERIALIZER_NULL = 0x03 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,
80+
SC_SERIALIZER_NULL = 0x02 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,
8381
/**< NULL object in serializer */
82+
SC_SERIALIZER_JSON_PARSE = 0x07 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL,
83+
/**< Fail to parse JSON object in serializer */
8484

8585
// Cache module
86-
SC_CACHE_NULL = 0x01 | SC_MODULE_CACHE | SC_SEVERITY_FATAL,
86+
SC_CACHE_NULL = 0x02 | SC_MODULE_CACHE | SC_SEVERITY_FATAL,
8787
/**< NULL parameters in cache */
88-
SC_CACHE_FAILED_RESPONSE = 0x02 | SC_MODULE_CACHE | SC_SEVERITY_FATAL,
88+
SC_CACHE_FAILED_RESPONSE = 0x04 | SC_MODULE_CACHE | SC_SEVERITY_FATAL,
8989
/**< Fail in cache operations */
9090

9191
// MAM module
9292
SC_MAM_OOM = 0x01 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
9393
/**< Fail to create mam object */
9494
SC_MAM_NULL = 0x02 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
9595
/**< NULL object in mam */
96-
SC_MAM_FAILED_RESPONSE = 0x03 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
96+
SC_MAM_NOT_FOUND = 0x03 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
97+
/**< Empty result from mam */
98+
SC_MAM_FAILED_RESPONSE = 0x04 | SC_MODULE_MAM | SC_SEVERITY_FATAL,
9799
/**< Error in mam response */
98100
} status_t;
99101

accelerator/server.cc

Lines changed: 60 additions & 19 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_ERROR_MASK) == 0x03) {
25+
http_ret = SC_HTTP_NOT_FOUND;
26+
cJSON_AddStringToObject(json_obj, "message", "Request not found");
27+
} else if ((ret & SC_ERROR_MASK) == 0x07) {
28+
http_ret = SC_HTTP_BAD_REQUEST;
29+
cJSON_AddStringToObject(json_obj, "message", "Invalid request header");
30+
} else {
31+
http_ret = SC_HTTP_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);
@@ -35,18 +56,17 @@ int main(int, char const**) {
3556
set_options_method_header(res);
3657
})
3758
.get([&](served::response& res, const served::request& req) {
59+
status_t ret = SC_OK;
3860
char* json_result = NULL;
3961

40-
api_receive_mam_message(&service, req.params["bundle"].c_str(),
41-
&json_result);
62+
ret = api_receive_mam_message(&service, req.params["bundle"].c_str(),
63+
&json_result);
64+
ret = set_response_content(ret, &json_result);
4265

4366
res.set_header("Content-Type", "application/json");
4467
res.set_header("Access-Control-Allow-Origin", "*");
45-
if (!json_result) {
46-
res.set_status(SC_NOT_FOUND);
47-
} else {
48-
res << json_result;
49-
}
68+
res.set_status(ret);
69+
res << json_result;
5070
});
5171

5272
/**
@@ -62,12 +82,15 @@ int main(int, char const**) {
6282
set_options_method_header(res);
6383
})
6484
.get([&](served::response& res, const served::request& req) {
85+
status_t ret = SC_OK;
6586
char* json_result;
6687

67-
api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
68-
&json_result);
88+
ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(),
89+
&json_result);
90+
ret = set_response_content(ret, &json_result);
6991
res.set_header("Content-Type", "application/json");
7092
res.set_header("Access-Control-Allow-Origin", "*");
93+
res.set_status(ret);
7194
res << json_result;
7295
});
7396

@@ -84,12 +107,15 @@ int main(int, char const**) {
84107
set_options_method_header(res);
85108
})
86109
.get([&](served::response& res, const served::request& req) {
110+
status_t ret = SC_OK;
87111
char* json_result;
88112

89-
api_get_transaction_object(&service, req.params["tx"].c_str(),
90-
&json_result);
113+
ret = api_get_transaction_object(&service, req.params["tx"].c_str(),
114+
&json_result);
115+
ret = set_response_content(ret, &json_result);
91116
res.set_header("Content-Type", "application/json");
92117
res.set_header("Access-Control-Allow-Origin", "*");
118+
res.set_status(ret);
93119
res << json_result;
94120
});
95121

@@ -106,12 +132,15 @@ int main(int, char const**) {
106132
set_options_method_header(res);
107133
})
108134
.get([&](served::response& res, const served::request& req) {
135+
status_t ret = SC_OK;
109136
char* json_result;
110137

111-
api_find_transactions_obj_by_tag(&service, req.params["tag"].c_str(),
112-
&json_result);
138+
ret = api_find_transactions_obj_by_tag(
139+
&service, req.params["tag"].c_str(), &json_result);
140+
ret = set_response_content(ret, &json_result);
113141
res.set_header("Content-Type", "application/json");
114142
res.set_header("Access-Control-Allow-Origin", "*");
143+
res.set_status(ret);
115144
res << json_result;
116145
});
117146

@@ -126,11 +155,14 @@ int main(int, char const**) {
126155
set_options_method_header(res);
127156
})
128157
.get([&](served::response& res, const served::request& req) {
158+
status_t ret = SC_OK;
129159
char* json_result;
130160

131-
api_get_tips_pair(&service, &json_result);
161+
ret = api_get_tips_pair(&service, &json_result);
162+
ret = set_response_content(ret, &json_result);
132163
res.set_header("Content-Type", "application/json");
133164
res.set_header("Access-Control-Allow-Origin", "*");
165+
res.set_status(ret);
134166
res << json_result;
135167
});
136168

@@ -145,11 +177,14 @@ int main(int, char const**) {
145177
set_options_method_header(res);
146178
})
147179
.get([&](served::response& res, const served::request& req) {
180+
status_t ret = SC_OK;
148181
char* json_result;
149182

150-
api_get_tips(&service, &json_result);
183+
ret = api_get_tips(&service, &json_result);
184+
ret = set_response_content(ret, &json_result);
151185
res.set_header("Content-Type", "application/json");
152186
res.set_header("Access-Control-Allow-Origin", "*");
187+
res.set_status(ret);
153188
res << json_result;
154189
});
155190

@@ -164,11 +199,14 @@ int main(int, char const**) {
164199
set_options_method_header(res);
165200
})
166201
.get([&](served::response& res, const served::request& req) {
202+
status_t ret = SC_OK;
167203
char* json_result;
168204

169-
api_generate_address(&service, &json_result);
205+
ret = api_generate_address(&service, &json_result);
206+
ret = set_response_content(ret, &json_result);
170207
res.set_header("Content-Type", "application/json");
171208
res.set_header("Access-Control-Allow-Origin", "*");
209+
res.set_status(ret);
172210
res << json_result;
173211
});
174212

@@ -183,6 +221,7 @@ int main(int, char const**) {
183221
set_options_method_header(res);
184222
})
185223
.post([&](served::response& res, const served::request& req) {
224+
status_t ret = SC_OK;
186225
char* json_result;
187226

188227
if (req.header("content-type").find("application/json") ==
@@ -192,10 +231,12 @@ int main(int, char const**) {
192231
"Invalid request header");
193232
json_result = cJSON_PrintUnformatted(json_obj);
194233

195-
res.set_status(SC_BAD_REQUEST);
234+
res.set_status(SC_HTTP_BAD_REQUEST);
196235
cJSON_Delete(json_obj);
197236
} else {
198-
api_send_transfer(&service, req.body().c_str(), &json_result);
237+
ret = api_send_transfer(&service, req.body().c_str(), &json_result);
238+
ret = set_response_content(ret, &json_result);
239+
res.set_status(ret);
199240
}
200241

201242
res.set_header("Content-Type", "application/json");
@@ -219,7 +260,7 @@ int main(int, char const**) {
219260
cJSON_AddStringToObject(json_obj, "message", "Invalid path");
220261
const char* json = cJSON_PrintUnformatted(json_obj);
221262

222-
res.set_status(SC_BAD_REQUEST);
263+
res.set_status(SC_HTTP_BAD_REQUEST);
223264
res.set_header("Content-Type", "application/json");
224265
res.set_header("Access-Control-Allow-Origin", "*");
225266
res << json;

0 commit comments

Comments
 (0)