Add exception handling

merge-requests/30/head
Bob Mottram 2024-07-18 13:55:47 +01:00
parent 78ac2e17ad
commit 8c30b537ab
1 changed files with 36 additions and 18 deletions

View File

@ -1179,8 +1179,13 @@ def is_suspended(base_dir: str, nickname: str) -> bool:
suspended_filename = data_dir(base_dir) + '/suspended.txt' suspended_filename = data_dir(base_dir) + '/suspended.txt'
if os.path.isfile(suspended_filename): if os.path.isfile(suspended_filename):
with open(suspended_filename, 'r', encoding='utf-8') as fp_susp: lines = []
lines = fp_susp.readlines() try:
with open(suspended_filename, 'r', encoding='utf-8') as fp_susp:
lines = fp_susp.readlines()
except OSError:
print('EX: is_suspended unable to read ' + suspended_filename)
for suspended in lines: for suspended in lines:
if suspended.strip('\n').strip('\r') == nickname: if suspended.strip('\n').strip('\r') == nickname:
return True return True
@ -1234,13 +1239,18 @@ def get_followers_of_person(base_dir: str,
continue continue
if not os.path.isfile(filename): if not os.path.isfile(filename):
continue continue
with open(filename, 'r', encoding='utf-8') as fp_following: try:
for following_handle in fp_following: with open(filename, 'r', encoding='utf-8') as fp_following:
following_handle2 = remove_eol(following_handle) for following_handle in fp_following:
if following_handle2 == handle: following_handle2 = remove_eol(following_handle)
if following_handle2 != handle:
continue
if account not in followers: if account not in followers:
followers.append(account) followers.append(account)
break break
except OSError as exc:
print('EX: get_followers_of_person unable to read ' +
filename + ' ' + str(exc))
break break
return followers return followers
@ -1308,13 +1318,14 @@ def remove_avatar_from_cache(base_dir: str, actor_str: str) -> None:
for extension in avatar_filename_extensions: for extension in avatar_filename_extensions:
avatar_filename = \ avatar_filename = \
base_dir + '/cache/avatars/' + actor_str + '.' + extension base_dir + '/cache/avatars/' + actor_str + '.' + extension
if os.path.isfile(avatar_filename): if not os.path.isfile(avatar_filename):
try: continue
os.remove(avatar_filename) try:
except OSError: os.remove(avatar_filename)
print('EX: remove_avatar_from_cache ' + except OSError:
'unable to delete cached avatar ' + print('EX: remove_avatar_from_cache ' +
str(avatar_filename)) 'unable to delete cached avatar ' +
str(avatar_filename))
def save_json(json_object: {}, filename: str) -> bool: def save_json(json_object: {}, filename: str) -> bool:
@ -2405,10 +2416,16 @@ def remove_moderation_post_from_index(base_dir: str, post_url: str,
return return
post_id = remove_id_ending(post_url) post_id = remove_id_ending(post_url)
if text_in_file(post_id, moderation_index_file): if text_in_file(post_id, moderation_index_file):
lines = []
try: try:
with open(moderation_index_file, 'r', with open(moderation_index_file, 'r', encoding='utf-8') as fp_mod1:
encoding='utf-8') as fp_mod1:
lines = fp_mod1.readlines() lines = fp_mod1.readlines()
except OSError as exc:
print('EX: remove_moderation_post_from_index unable to read ' +
moderation_index_file + ' ' + str(exc))
if lines:
try:
with open(moderation_index_file, 'w+', with open(moderation_index_file, 'w+',
encoding='utf-8') as fp_mod2: encoding='utf-8') as fp_mod2:
for line in lines: for line in lines:
@ -2418,9 +2435,10 @@ def remove_moderation_post_from_index(base_dir: str, post_url: str,
if debug: if debug:
print('DEBUG: removed ' + post_id + print('DEBUG: removed ' + post_id +
' from moderation index') ' from moderation index')
except OSError as ex: except OSError as exc:
print('EX: remove_moderation_post_from_index unable to read ' + print('EX: ' +
moderation_index_file + ' ' + str(ex)) 'remove_moderation_post_from_index unable to write ' +
moderation_index_file + ' ' + str(exc))
def _is_reply_to_blog_post(base_dir: str, nickname: str, domain: str, def _is_reply_to_blog_post(base_dir: str, nickname: str, domain: str,