improved exception handling on failed HTTP requests
This commit is contained in:
parent
319ee643ad
commit
f5931dcc7e
4 changed files with 34 additions and 29 deletions
|
@ -67,6 +67,6 @@ json_object * api_json_tuples(buffer_t *buf, reading_t *first, reading_t *last);
|
|||
/**
|
||||
* Parses JSON encoded exception and stores describtion in err
|
||||
*/
|
||||
void api_parse_exception(CURLresponse response, char *err, size_t n);
|
||||
int api_parse_exception(CURLresponse response, char *err, size_t n);
|
||||
|
||||
#endif /* _API_H_ */
|
||||
|
|
17
src/api.c
17
src/api.c
|
@ -138,14 +138,18 @@ void api_free(api_handle_t *api) {
|
|||
curl_slist_free_all(api->headers);
|
||||
}
|
||||
|
||||
void api_parse_exception(CURLresponse response, char *err, size_t n) {
|
||||
int api_parse_exception(CURLresponse response, char *err, size_t n) {
|
||||
struct json_tokener *json_tok;
|
||||
struct json_object *json_obj;
|
||||
|
||||
json_tok = json_tokener_new();
|
||||
json_obj = json_tokener_parse_ex(json_tok, response.data, response.size);
|
||||
|
||||
if (json_tok->err == json_tokener_success) {
|
||||
if (json_tok->err != json_tokener_success) {
|
||||
json_tokener_free(json_tok);
|
||||
return ERR;
|
||||
}
|
||||
|
||||
json_obj = json_object_object_get(json_obj, "exception");
|
||||
|
||||
if (json_obj) {
|
||||
|
@ -155,14 +159,13 @@ void api_parse_exception(CURLresponse response, char *err, size_t n) {
|
|||
);
|
||||
}
|
||||
else {
|
||||
strncpy(err, "missing exception", n);
|
||||
}
|
||||
}
|
||||
else {
|
||||
strncpy(err, json_tokener_errors[json_tok->err], n);
|
||||
json_object_put(json_obj);
|
||||
json_tokener_free(json_tok);
|
||||
return ERR;
|
||||
}
|
||||
|
||||
json_object_put(json_obj);
|
||||
json_tokener_free(json_tok);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,16 +36,16 @@ char * reading_id_registry(const char *str) {
|
|||
char *found = NULL;
|
||||
|
||||
/* linear search in string list */
|
||||
foreach(strings, it, char *) {
|
||||
if (strcmp(it, str) == 0) {
|
||||
found = it;
|
||||
foreach(strings, it, const char *) {
|
||||
if (strcmp(*it, str) == 0) {
|
||||
found = *it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
found = strdup(str);
|
||||
list_push(strings, found);
|
||||
list_push(&strings, found);
|
||||
}
|
||||
|
||||
return found;
|
||||
|
|
|
@ -199,19 +199,21 @@ void * logging_thread(void *arg) {
|
|||
curl_easy_getinfo(api.curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||
|
||||
/* check response */
|
||||
if (curl_code == CURLE_OK && http_code == 200) { /* everything is ok */
|
||||
print(log_debug, "Request succeeded with code: %i", ch, http_code);
|
||||
ch->buffer.sent = last->next;
|
||||
}
|
||||
else { /* error */
|
||||
if (curl_code != CURLE_OK) {
|
||||
print(log_error, "CURL: %s", ch, curl_easy_strerror(curl_code));
|
||||
}
|
||||
else if (http_code != 200) {
|
||||
char err[255];
|
||||
api_parse_exception(response, err, 255);
|
||||
print(log_error, "Error from middleware: %s", ch, err);
|
||||
char exception[255];
|
||||
if (api_parse_exception(response, exception, 255) == SUCCESS) {
|
||||
print(log_error, "Request failed: [%i] %s", ch, http_code, exception);
|
||||
}
|
||||
else {
|
||||
print(log_error, "Request failed: %i", ch, http_code);
|
||||
}
|
||||
}
|
||||
else {
|
||||
print(log_debug, "Request succeeded: %i", ch, http_code);
|
||||
ch->buffer.sent = last->next;
|
||||
}
|
||||
|
||||
/* householding */
|
||||
|
|
Loading…
Add table
Reference in a new issue