improved exception handling on failed HTTP requests

This commit is contained in:
Steffen Vogel 2012-03-12 16:12:24 +01:00
parent 319ee643ad
commit f5931dcc7e
4 changed files with 34 additions and 29 deletions

View File

@ -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_ */

View File

@ -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;
}

View File

@ -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;

View File

@ -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);