From f71425528ef93a938e5cbea65318dc6f3a552a35 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sat, 7 Jan 2023 23:46:28 +0100 Subject: [PATCH] add some exception handling for problems --- .../brewserverblocklist.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/brewserverblocklist/brewserverblocklist.py b/src/brewserverblocklist/brewserverblocklist.py index 56f6d8f..633e17a 100644 --- a/src/brewserverblocklist/brewserverblocklist.py +++ b/src/brewserverblocklist/brewserverblocklist.py @@ -32,6 +32,7 @@ class BrewBlocklist(): self.auto_accept = auto_accept self.auto_accept_direction = auto_accept_direction self.safe_harbor = [] + self.error = [] config = configparser.RawConfigParser() config.read(configfile) for section in config.sections(): @@ -62,6 +63,9 @@ class BrewBlocklist(): if source['type'] == 'friendica': # Friendica publishes the blocklist as CSV file requ = requests.get('https://{}/blocklist/domain/download'.format(source['url'])) + if not requ.status_code == requests.codes.ok: + self.error.append('The request to {} failed'.format(sources['url'])) + break for line in requ.text.split('\n'): try: pattern, reason = line.split(',') @@ -75,9 +79,15 @@ class BrewBlocklist(): elif source['type'] == 'mastodon': # Mastodon has an API endpoint that contains the information requ = requests.get('https://{}//api/v1/instance/domain_blocks'.format(source['url'])) - for item in requ.json(): - self.blocklist[item['domain']] = self.blocklist.get(item['domain'], 0) + source['trust'] - self.reasons[item['domain']] = self.reasons.get(item['domain'], item['comment']) + if not requ.status_code == requests.codes.ok: + self.error.append('The request to {} failed'.format(sources['url'])) + break + try: + for item in requ.json(): + self.blocklist[item['domain']] = self.blocklist.get(item['domain'], 0) + source['trust'] + self.reasons[item['domain']] = self.reasons.get(item['domain'], item['comment']) + except: + self.error.append('{} returned no valid json to the API call'.format(source['url'])) else: raise ValueError('{} is not a supported node type, check your config file'.format(source['type'])) @@ -129,6 +139,9 @@ class BrewBlocklist(): if self.outputfile: sys.stdout = orig_stdout out_file.close() + if len(self.error): + print("\n\nWhile creating the blocklist the following problems occured:") + print("\n".join(self.error)) def main(): """