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