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