Catch errors thrown by check_response_code 50/62350/1
authorEric Ball <eball@linuxfoundation.org>
Wed, 20 Nov 2019 02:02:24 +0000 (18:02 -0800)
committerEric Ball <eball@linuxfoundation.org>
Wed, 20 Nov 2019 02:13:09 +0000 (18:13 -0800)
For the current methods in lfidapi, we don't need to see the stack
trace or raise the error, so we should just catch and print it, and
then exit 1.
This also includes some minor cleanup.

Issue: RELENG-2024
Change-Id: Ife7f9b82be148a208c66134ef912bc091afac041
Signed-off-by: Eric Ball <eball@linuxfoundation.org>
lftools/lfidapi.py
releasenotes/notes/handle-lfidapi-errors-433cac02fc5e5e00.yaml [new file with mode: 0644]

index af4ae5a..755496f 100755 (executable)
@@ -50,7 +50,11 @@ def helper_search_members(group):
     url = PARSE(url, group)
     headers = {'Authorization': 'Bearer ' + access_token}
     response = requests.get(url, headers=headers)
-    check_response_code(response)
+    try:
+        check_response_code(response)
+    except requests.HTTPError as e:
+        log.error(e)
+        exit(1)
     result = (response.json())
     members = result["members"]
     log.debug(json.dumps(members, indent=4, sort_keys=True))
@@ -69,7 +73,11 @@ def helper_user(user, group, delete):
     else:
         log.info('Adding %s to %s' % (user, group))
         response = requests.put(url, json=data, headers=headers)
-    check_response_code(response)
+    try:
+        check_response_code(response)
+    except requests.HTTPError as e:
+        log.error(e)
+        exit(1)
     result = (response.json())
     log.debug(json.dumps(result, indent=4, sort_keys=True))
 
@@ -85,7 +93,11 @@ def helper_invite(email, group):
     if validate_email(email):
         log.info('Inviting %s to join %s' % (email, group))
         response = requests.post(url, json=data, headers=headers)
-        check_response_code(response)
+        try:
+            check_response_code(response)
+        except requests.HTTPError as e:
+            log.error(e)
+            exit(1)
         result = (response.json())
         log.debug(json.dumps(result, indent=4, sort_keys=True))
     else:
@@ -97,7 +109,7 @@ def helper_create_group(group):
     """Create group."""
     response_code = helper_check_group_exists(group)
     if response_code == 200:
-        log.error("Group %s already exists exiting..." % group)
+        log.error("Group %s already exists. Exiting..." % group)
     else:
         access_token, url = oauth_helper()
         url = '{}/'.format(url)
@@ -106,7 +118,11 @@ def helper_create_group(group):
         log.debug(data)
         log.info('Creating group %s' % group)
         response = requests.post(url, json=data, headers=headers)
-        check_response_code(response)
+        try:
+            check_response_code(response)
+        except requests.HTTPError as e:
+            log.error(e)
+            exit(1)
         result = (response.json())
         log.debug(json.dumps(result, indent=4, sort_keys=True))
 
@@ -159,7 +175,7 @@ def helper_match_ldap_to_info(info_file, group, githuborg, noop):
 
     for user in all_users:
         removed_by_patch = [item for item in ldap_committers if item not in info_committers]
-        if (user in removed_by_patch):
+        if user in removed_by_patch:
             log.info("%s found in group %s " % (user, group))
             if noop is False:
                 log.info("Removing user %s from group %s" % (user, group))
@@ -170,7 +186,7 @@ def helper_match_ldap_to_info(info_file, group, githuborg, noop):
                     helper_user(user, group, "--delete")
 
         added_by_patch = [item for item in info_committers if item not in ldap_committers]
-        if (user in added_by_patch):
+        if user in added_by_patch:
             log.info("%s not found in group %s" % (user, group))
             if noop is False:
                 log.info("Adding user %s to group %s" % (user, group))
diff --git a/releasenotes/notes/handle-lfidapi-errors-433cac02fc5e5e00.yaml b/releasenotes/notes/handle-lfidapi-errors-433cac02fc5e5e00.yaml
new file mode 100644 (file)
index 0000000..70e31c9
--- /dev/null
@@ -0,0 +1,4 @@
+---
+fixes:
+  - |
+    Catch and print errors thrown by check_response_code in lftools/lfidapi.py.