Skip to content

Commit 986aee0

Browse files
author
Makoto Koishi
committed
HTTP error handling modified to continue until no Notifications
1 parent 117c203 commit 986aee0

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

examples/filtering_notifications.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import json
6060
import traceback
6161
import re
62-
from requests import HTTPError
62+
from requests import RequestException
6363
from blackduck import Client
6464
from ast import literal_eval
6565

@@ -120,24 +120,32 @@ def main():
120120

121121
user = get_user(hub_client, user_name)
122122
if user:
123-
number_of_seen = 0
124-
for notification in read_notifications(
125-
hub_client,
126-
notification_filters,
127-
user,
128-
vuln_source=vuln_source):
129-
user_id = re.split("/", user['_meta']['href'])[-1]
130-
res = update_user_notification(hub_client, user_id, notification)
131-
number_of_seen += 1
132-
content = literal_eval(res.content.decode("UTF-8"))
133-
logging.info(f"Notification state is set to SEEN for {content['type']}")
134-
logging.info(f"Finished. Number of Notifications which are set to SEEN is {number_of_seen}")
123+
set_to_seen = 0
124+
failed = 0
125+
while True:
126+
try:
127+
for notification in read_notifications(
128+
hub_client,
129+
notification_filters,
130+
user,
131+
vuln_source=vuln_source):
132+
user_id = re.split("/", user['_meta']['href'])[-1]
133+
res = update_user_notification(hub_client, user_id, notification)
134+
set_to_seen += 1
135+
content = literal_eval(res.content.decode("UTF-8"))
136+
logging.info(f"Notification state is set to SEEN for {content['type']}")
137+
except RequestException as err:
138+
failed += 1
139+
logging.error(f"Failed to read or update notification and the reason is {str(err)}")
140+
continue
141+
logging.info(f"=== Updating Notifications Finished ===")
142+
logging.info(f"{set_to_seen} Notifications are set to SEEN.")
143+
logging.info(f"{failed} Notifications failed to read or update the state.")
144+
break
135145
else:
136146
logging.error(f"User not found for {user_name}")
137-
except HTTPError as err:
138-
hub_client.http_error_handler(err)
139147
except Exception as err:
140-
logging.error(f"Failed to perform the task. See the stack trace")
148+
logging.error(f"Failed to perform the task with exception {str(err)}. See also the stack trace")
141149
traceback.print_exc()
142150

143151
if __name__ == '__main__':

0 commit comments

Comments
 (0)