@@ -14,6 +14,27 @@ void set_options_method_header(served::response& res) {
14
14
res.set_header (" Access-Control-Max-Age" , " 86400" );
15
15
}
16
16
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
+
17
38
int main (int , char const **) {
18
39
served::multiplexer mux;
19
40
mux.use_after (served::plugin::access_log);
@@ -62,12 +83,15 @@ int main(int, char const**) {
62
83
set_options_method_header (res);
63
84
})
64
85
.get ([&](served::response& res, const served::request& req) {
86
+ status_t ret = SC_OK;
65
87
char * json_result;
66
88
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);
69
92
res.set_header (" Content-Type" , " application/json" );
70
93
res.set_header (" Access-Control-Allow-Origin" , " *" );
94
+ res.set_status (ret);
71
95
res << json_result;
72
96
});
73
97
@@ -84,12 +108,15 @@ int main(int, char const**) {
84
108
set_options_method_header (res);
85
109
})
86
110
.get ([&](served::response& res, const served::request& req) {
111
+ status_t ret = SC_OK;
87
112
char * json_result;
88
113
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);
91
117
res.set_header (" Content-Type" , " application/json" );
92
118
res.set_header (" Access-Control-Allow-Origin" , " *" );
119
+ res.set_status (ret);
93
120
res << json_result;
94
121
});
95
122
@@ -106,12 +133,15 @@ int main(int, char const**) {
106
133
set_options_method_header (res);
107
134
})
108
135
.get ([&](served::response& res, const served::request& req) {
136
+ status_t ret = SC_OK;
109
137
char * json_result;
110
138
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);
113
142
res.set_header (" Content-Type" , " application/json" );
114
143
res.set_header (" Access-Control-Allow-Origin" , " *" );
144
+ res.set_status (ret);
115
145
res << json_result;
116
146
});
117
147
@@ -126,11 +156,14 @@ int main(int, char const**) {
126
156
set_options_method_header (res);
127
157
})
128
158
.get ([&](served::response& res, const served::request& req) {
159
+ status_t ret = SC_OK;
129
160
char * json_result;
130
161
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);
132
164
res.set_header (" Content-Type" , " application/json" );
133
165
res.set_header (" Access-Control-Allow-Origin" , " *" );
166
+ res.set_status (ret);
134
167
res << json_result;
135
168
});
136
169
@@ -145,11 +178,14 @@ int main(int, char const**) {
145
178
set_options_method_header (res);
146
179
})
147
180
.get ([&](served::response& res, const served::request& req) {
181
+ status_t ret = SC_OK;
148
182
char * json_result;
149
183
150
- api_get_tips (&service, &json_result);
184
+ ret = api_get_tips (&service, &json_result);
185
+ ret = set_response_content (ret, &json_result);
151
186
res.set_header (" Content-Type" , " application/json" );
152
187
res.set_header (" Access-Control-Allow-Origin" , " *" );
188
+ res.set_status (ret);
153
189
res << json_result;
154
190
});
155
191
@@ -164,11 +200,14 @@ int main(int, char const**) {
164
200
set_options_method_header (res);
165
201
})
166
202
.get ([&](served::response& res, const served::request& req) {
203
+ status_t ret = SC_OK;
167
204
char * json_result;
168
205
169
- api_generate_address (&service, &json_result);
206
+ ret = api_generate_address (&service, &json_result);
207
+ ret = set_response_content (ret, &json_result);
170
208
res.set_header (" Content-Type" , " application/json" );
171
209
res.set_header (" Access-Control-Allow-Origin" , " *" );
210
+ res.set_status (ret);
172
211
res << json_result;
173
212
});
174
213
@@ -183,6 +222,7 @@ int main(int, char const**) {
183
222
set_options_method_header (res);
184
223
})
185
224
.post ([&](served::response& res, const served::request& req) {
225
+ status_t ret = SC_OK;
186
226
char * json_result;
187
227
188
228
if (req.header (" content-type" ).find (" application/json" ) ==
@@ -195,7 +235,9 @@ int main(int, char const**) {
195
235
res.set_status (SC_BAD_REQUEST);
196
236
cJSON_Delete (json_obj);
197
237
} 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);
199
241
}
200
242
201
243
res.set_header (" Content-Type" , " application/json" );
0 commit comments