fc77ae9
From 167ebb0a8930dcd19c4078773da8c580785053d9 Mon Sep 17 00:00:00 2001
fc77ae9
From: Matej Habrnal <mhabrnal@redhat.com>
fc77ae9
Date: Wed, 26 Aug 2015 13:41:27 +0200
fc77ae9
Subject: [PATCH] bodhi: add parsing of error responses
fc77ae9
fc77ae9
Resolves: rhbz#1256493
fc77ae9
fc77ae9
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
fc77ae9
---
fc77ae9
 src/plugins/bodhi.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
fc77ae9
 1 file changed, 69 insertions(+), 1 deletion(-)
fc77ae9
fc77ae9
diff --git a/src/plugins/bodhi.c b/src/plugins/bodhi.c
fc77ae9
index 9149347..b348a26 100644
fc77ae9
--- a/src/plugins/bodhi.c
fc77ae9
+++ b/src/plugins/bodhi.c
fc77ae9
@@ -217,6 +217,58 @@ static void print_bodhi(struct bodhi *b)
fc77ae9
 }
fc77ae9
 #endif
fc77ae9
 
fc77ae9
+/* bodhi returns following json structure in case of error
fc77ae9
+{
fc77ae9
+   "status": "error",
fc77ae9
+   "errors":
fc77ae9
+              [
fc77ae9
+                {
fc77ae9
+                   "location": "querystring",
fc77ae9
+                   "name": "releases",
fc77ae9
+                   "description": "Invalid releases specified: Rawhide"
fc77ae9
+                }
fc77ae9
+              ]
fc77ae9
+}
fc77ae9
+*/
fc77ae9
+static void bodhi_print_errors_from_json(json_object *json)
fc77ae9
+{
fc77ae9
+
fc77ae9
+    json_object *errors_array = NULL;
fc77ae9
+    bodhi_read_value(json, "errors", &errors_array, BODHI_READ_JSON_OBJ);
fc77ae9
+    if (!errors_array)
fc77ae9
+    {
fc77ae9
+        error_msg("Error: unable to read 'errors' array from json");
fc77ae9
+        return;
fc77ae9
+    }
fc77ae9
+
fc77ae9
+    int errors_len = json_object_array_length(errors_array);
fc77ae9
+    for (int i = 0; i < errors_len; ++i)
fc77ae9
+    {
fc77ae9
+        json_object *error = json_object_array_get_idx(errors_array, i);
fc77ae9
+        if (!error)
fc77ae9
+        {
fc77ae9
+            error_msg("Error: unable to get 'error[%d]'", i);
fc77ae9
+            json_object_put(errors_array);
fc77ae9
+            return;
fc77ae9
+        }
fc77ae9
+
fc77ae9
+        char *desc_item = NULL;
fc77ae9
+        bodhi_read_value(error, "description", &desc_item, BODHI_READ_STR);
fc77ae9
+        if (!desc_item)
fc77ae9
+        {
fc77ae9
+            error_msg("Error: unable to get 'description' from 'error[%d]'", i);
fc77ae9
+            continue;
fc77ae9
+        }
fc77ae9
+
fc77ae9
+        error_msg("Error: %s", desc_item);
fc77ae9
+        json_object_put(error);
fc77ae9
+        free(desc_item);
fc77ae9
+    }
fc77ae9
+
fc77ae9
+    json_object_put(errors_array);
fc77ae9
+    return;
fc77ae9
+}
fc77ae9
+
fc77ae9
 static GHashTable *bodhi_parse_json(json_object *json, const char *release)
fc77ae9
 {
fc77ae9
 
fc77ae9
@@ -326,7 +378,7 @@ static GHashTable *bodhi_query_list(const char *query, const char *release)
fc77ae9
     get(post_state, bodhi_url_bugs, "application/x-www-form-urlencoded",
fc77ae9
                      headers);
fc77ae9
 
fc77ae9
-    if (post_state->http_resp_code != 200)
fc77ae9
+    if (post_state->http_resp_code != 200 && post_state->http_resp_code != 400)
fc77ae9
     {
fc77ae9
         char *errmsg = post_state->curl_error_msg;
fc77ae9
         if (errmsg && errmsg[0])
fc77ae9
@@ -340,6 +392,22 @@ static GHashTable *bodhi_query_list(const char *query, const char *release)
fc77ae9
     if (is_error(json))
fc77ae9
         error_msg_and_die("fatal: unable parse response from bodhi server");
fc77ae9
 
fc77ae9
+    /* we must check the http_resp_code because only error responses contain
fc77ae9
+     * 'status' item. 'bodhi_read_value' function prints an error message in
fc77ae9
+     * the case it did not found the item */
fc77ae9
+    if (post_state->http_resp_code != 200)
fc77ae9
+    {
fc77ae9
+        char *status_item = NULL;
fc77ae9
+        bodhi_read_value(json, "status", &status_item, BODHI_READ_STR);
fc77ae9
+        if (status_item != NULL && strcmp(status_item, "error") == 0)
fc77ae9
+        {
fc77ae9
+            free(status_item);
fc77ae9
+            bodhi_print_errors_from_json(json);
fc77ae9
+            json_object_put(json);
fc77ae9
+            xfunc_die(); // error_msg are printed in bodhi_print_errors_from_json
fc77ae9
+        }
fc77ae9
+    }
fc77ae9
+
fc77ae9
     GHashTable *bodhi_table = bodhi_parse_json(json, release);
fc77ae9
     json_object_put(json);
fc77ae9
     free_post_state(post_state);
fc77ae9
-- 
fc77ae9
2.5.0
fc77ae9