diff --git a/include/api.h b/include/api.h index 25cf22a..40c0468 100644 --- a/include/api.h +++ b/include/api.h @@ -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_ */ diff --git a/src/api.c b/src/api.c index d255e3c..2bb536c 100644 --- a/src/api.c +++ b/src/api.c @@ -138,31 +138,34 @@ 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) { - json_obj = json_object_object_get(json_obj, "exception"); + if (json_tok->err != json_tokener_success) { + json_tokener_free(json_tok); + return ERR; + } - if (json_obj) { - snprintf(err, n, "%s: %s", - json_object_get_string(json_object_object_get(json_obj, "type")), - json_object_get_string(json_object_object_get(json_obj, "message")) - ); - } - else { - strncpy(err, "missing exception", n); - } + json_obj = json_object_object_get(json_obj, "exception"); + + if (json_obj) { + snprintf(err, n, "%s: %s", + json_object_get_string(json_object_object_get(json_obj, "type")), + json_object_get_string(json_object_object_get(json_obj, "message")) + ); } 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; } diff --git a/src/reading.c b/src/reading.c index a4dcfcb..13765de 100644 --- a/src/reading.c +++ b/src/reading.c @@ -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; diff --git a/src/threads.c b/src/threads.c index 63e82ca..eb99beb 100644 --- a/src/threads.c +++ b/src/threads.c @@ -199,20 +199,22 @@ 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; + if (curl_code != CURLE_OK) { + print(log_error, "CURL: %s", ch, curl_easy_strerror(curl_code)); } - else { /* error */ - if (curl_code != CURLE_OK) { - print(log_error, "CURL: %s", ch, curl_easy_strerror(curl_code)); + else if (http_code != 200) { + char exception[255]; + if (api_parse_exception(response, exception, 255) == SUCCESS) { + print(log_error, "Request failed: [%i] %s", ch, http_code, exception); } - else if (http_code != 200) { - char err[255]; - api_parse_exception(response, err, 255); - print(log_error, "Error from middleware: %s", ch, err); + 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 */ free(response.data);