From 8bbacc3da6909019009ab7f10db3b4d54fd48020 Mon Sep 17 00:00:00 2001 From: freamon Date: Tue, 8 Oct 2024 03:26:40 +0000 Subject: [PATCH] API: let exceptions bubble-up instead of unnecessary catch and raise --- app/api/alpha/utils/community.py | 73 +++++++----------------------- app/api/alpha/utils/post.py | 76 +++++++++----------------------- app/api/alpha/utils/reply.py | 62 ++++++++------------------ app/api/alpha/utils/site.py | 5 +-- app/api/alpha/utils/user.py | 45 +++++++------------ app/api/alpha/views.py | 20 +++------ app/shared/auth.py | 7 +-- app/shared/community.py | 20 ++------- app/shared/post.py | 20 ++------- app/shared/reply.py | 20 ++------- app/shared/user.py | 10 +---- 11 files changed, 94 insertions(+), 264 deletions(-) diff --git a/app/api/alpha/utils/community.py b/app/api/alpha/utils/community.py index b573af73..6a5337b5 100644 --- a/app/api/alpha/utils/community.py +++ b/app/api/alpha/utils/community.py @@ -25,13 +25,7 @@ def get_community_list(auth, data): page = int(data['page']) if data and 'page' in data else 1 limit = int(data['limit']) if data and 'limit' in data else 10 - if auth: - try: - user_id = authorise_api_user(auth) - except: - raise - else: - user_id = None + user_id = authorise_api_user(auth) if auth else None communities = cached_community_list(type, user_id) @@ -57,71 +51,36 @@ def get_community(auth, data): elif 'name' in data: community = data['name'] - if auth: - try: - user_id = authorise_api_user(auth) - except: - raise - else: - user_id = None + user_id = authorise_api_user(auth) if auth else None - try: - community_json = community_view(community=community, variant=3, stub=False, user_id=user_id) - return community_json - except: - raise + community_json = community_view(community=community, variant=3, stub=False, user_id=user_id) + return community_json # would be in app/constants.py SRC_API = 3 def post_community_follow(auth, data): - try: - required(['community_id', 'follow'], data) - integer_expected(['community_id'], data) - boolean_expected(['follow'], data) - except: - raise + required(['community_id', 'follow'], data) + integer_expected(['community_id'], data) + boolean_expected(['follow'], data) community_id = data['community_id'] follow = data['follow'] - if auth: - try: - user_id = authorise_api_user(auth) - except: - raise - else: - user_id = None - - try: - if follow == True: - user_id = join_community(community_id, SRC_API, auth) - else: - user_id = leave_community(community_id, SRC_API, auth) - community_json = community_view(community=community_id, variant=4, stub=False, user_id=user_id) - return community_json - except: - raise + user_id = join_community(community_id, SRC_API, auth) if follow else leave_community(community_id, SRC_API, auth) + community_json = community_view(community=community_id, variant=4, stub=False, user_id=user_id) + return community_json def post_community_block(auth, data): - try: - required(['community_id', 'block'], data) - integer_expected(['community_id'], data) - boolean_expected(['block'], data) - except: - raise + required(['community_id', 'block'], data) + integer_expected(['community_id'], data) + boolean_expected(['block'], data) community_id = data['community_id'] block = data['block'] - try: - if block == True: - user_id = block_community(community_id, SRC_API, auth) - else: - user_id = unblock_community(community_id, SRC_API, auth) - community_json = community_view(community=community_id, variant=5, user_id=user_id) - return community_json - except: - raise + user_id = block_community(community_id, SRC_API, auth) if block else unblock_community(community_id, SRC_API, auth) + community_json = community_view(community=community_id, variant=5, user_id=user_id) + return community_json diff --git a/app/api/alpha/utils/post.py b/app/api/alpha/utils/post.py index a6c7e0a5..dd8d221f 100644 --- a/app/api/alpha/utils/post.py +++ b/app/api/alpha/utils/post.py @@ -55,10 +55,7 @@ def get_post_list(auth, data, user_id=None): limit = int(data['limit']) if data and 'limit' in data else 10 if auth: - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) # user_id: the logged in user # person_id: the author of the posts being requested @@ -92,13 +89,7 @@ def get_post(auth, data): id = int(data['id']) - if auth: - try: - user_id = authorise_api_user(auth) - except Exception as e: - raise e - else: - user_id = None + user_id = authorise_api_user(auth) if auth else None post_json = post_view(post=id, variant=3, user_id=user_id) if post_json: @@ -111,11 +102,8 @@ def get_post(auth, data): SRC_API = 3 def post_post_like(auth, data): - try: - required(['post_id', 'score'], data) - integer_expected(['post_id', 'score'], data) - except: - raise + required(['post_id', 'score'], data) + integer_expected(['post_id', 'score'], data) post_id = data['post_id'] score = data['score'] @@ -127,58 +115,34 @@ def post_post_like(auth, data): score = 0 direction = 'reversal' - try: - user_id = vote_for_post(post_id, direction, SRC_API, auth) - cache.delete_memoized(cached_post_list) - post_json = post_view(post=post_id, variant=4, user_id=user_id, my_vote=score) - return post_json - except: - raise + user_id = vote_for_post(post_id, direction, SRC_API, auth) + cache.delete_memoized(cached_post_list) + post_json = post_view(post=post_id, variant=4, user_id=user_id, my_vote=score) + return post_json def put_post_save(auth, data): - try: - required(['post_id', 'save'], data) - integer_expected(['post_id'], data) - boolean_expected(['save'], data) - except: - raise + required(['post_id', 'save'], data) + integer_expected(['post_id'], data) + boolean_expected(['save'], data) post_id = data['post_id'] save = data['save'] - try: - if save is True: - user_id = bookmark_the_post(post_id, SRC_API, auth) - else: - user_id = remove_the_bookmark_from_post(post_id, SRC_API, auth) - post_json = post_view(post=post_id, variant=4, user_id=user_id) - return post_json - except: - raise + user_id = bookmark_the_post(post_id, SRC_API, auth) if save else remove_the_bookmark_from_post(post_id, SRC_API, auth) + post_json = post_view(post=post_id, variant=4, user_id=user_id) + return post_json def put_post_subscribe(auth, data): - try: - required(['post_id', 'subscribe'], data) - integer_expected(['post_id'], data) - boolean_expected(['subscribe'], data) - except: - raise + required(['post_id', 'subscribe'], data) + integer_expected(['post_id'], data) + boolean_expected(['subscribe'], data) post_id = data['post_id'] subscribe = data['subscribe'] # not actually processed - is just a toggle - try: - user_id = toggle_post_notification(post_id, SRC_API, auth) - post_json = post_view(post=post_id, variant=4, user_id=user_id) - return post_json - except: - raise - - - - - - + user_id = toggle_post_notification(post_id, SRC_API, auth) + post_json = post_view(post=post_id, variant=4, user_id=user_id) + return post_json diff --git a/app/api/alpha/utils/reply.py b/app/api/alpha/utils/reply.py index e459eb06..90f1f856 100644 --- a/app/api/alpha/utils/reply.py +++ b/app/api/alpha/utils/reply.py @@ -42,10 +42,7 @@ def get_reply_list(auth, data, user_id=None): raise Exception('missing_parameters') else: if auth: - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) replies = cached_reply_list(post_id, person_id, sort, max_depth, user_id) # user_id: the logged in user @@ -72,11 +69,8 @@ def get_reply_list(auth, data, user_id=None): SRC_API = 3 def post_reply_like(auth, data): - try: - required(['comment_id', 'score'], data) - integer_expected(['comment_id', 'score'], data) - except: - raise + required(['comment_id', 'score'], data) + integer_expected(['comment_id', 'score'], data) score = data['score'] reply_id = data['comment_id'] @@ -88,51 +82,33 @@ def post_reply_like(auth, data): score = 0 direction = 'reversal' - try: - user_id = vote_for_reply(reply_id, direction, SRC_API, auth) - cache.delete_memoized(cached_reply_list) - reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id, my_vote=score) - return reply_json - except: - raise + user_id = vote_for_reply(reply_id, direction, SRC_API, auth) + cache.delete_memoized(cached_reply_list) + reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id, my_vote=score) + return reply_json def put_reply_save(auth, data): - try: - required(['comment_id', 'save'], data) - integer_expected(['comment_id'], data) - boolean_expected(['save'], data) - except: - raise + required(['comment_id', 'save'], data) + integer_expected(['comment_id'], data) + boolean_expected(['save'], data) reply_id = data['comment_id'] save = data['save'] - try: - if save is True: - user_id = bookmark_the_post_reply(reply_id, SRC_API, auth) - else: - user_id = remove_the_bookmark_from_post_reply(reply_id, SRC_API, auth) - reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id) - return reply_json - except: - raise + user_id = bookmark_the_post_reply(reply_id, SRC_API, auth) if save else remove_the_bookmark_from_post_reply(reply_id, SRC_API, auth) + reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id) + return reply_json def put_reply_subscribe(auth, data): - try: - required(['comment_id', 'subscribe'], data) - integer_expected(['comment_id'], data) - boolean_expected(['subscribe'], data) - except: - raise + required(['comment_id', 'subscribe'], data) + integer_expected(['comment_id'], data) + boolean_expected(['subscribe'], data) reply_id = data['comment_id'] subscribe = data['subscribe'] # not actually processed - is just a toggle - try: - user_id = toggle_post_reply_notification(reply_id, SRC_API, auth) - reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id) - return reply_json - except: - raise + user_id = toggle_post_reply_notification(reply_id, SRC_API, auth) + reply_json = reply_view(reply=reply_id, variant=4, user_id=user_id) + return reply_json diff --git a/app/api/alpha/utils/site.py b/app/api/alpha/utils/site.py index 0bc56c75..40a512cd 100644 --- a/app/api/alpha/utils/site.py +++ b/app/api/alpha/utils/site.py @@ -13,10 +13,7 @@ def users_total(): def get_site(auth): if auth: - try: - user = authorise_api_user(auth, return_type='model') - except Exception as e: - raise e + user = authorise_api_user(auth, return_type='model') else: user = None diff --git a/app/api/alpha/utils/user.py b/app/api/alpha/utils/user.py index 84459b24..d3278391 100644 --- a/app/api/alpha/utils/user.py +++ b/app/api/alpha/utils/user.py @@ -19,48 +19,33 @@ def get_user(auth, data): user_id = None if auth: - try: - user_id = authorise_api_user(auth) - if 'username' in data: - data['person_id'] = user_id - person_id = int(user_id) - auth = None # avoid authenticating user again in get_post_list and get_reply_list - except Exception as e: - raise e + user_id = authorise_api_user(auth) + if 'username' in data: + data['person_id'] = user_id + person_id = int(user_id) + auth = None # avoid authenticating user again in get_post_list and get_reply_list # bit unusual. have to help construct the json here rather than in views, to avoid circular dependencies post_list = get_post_list(auth, data, user_id) reply_list = get_reply_list(auth, data, user_id) - try: - user_json = user_view(user=person_id, variant=3) - user_json['posts'] = post_list['posts'] - user_json['comments'] = reply_list['comments'] - return user_json - except: - raise + user_json = user_view(user=person_id, variant=3) + user_json['posts'] = post_list['posts'] + user_json['comments'] = reply_list['comments'] + return user_json # would be in app/constants.py SRC_API = 3 def post_user_block(auth, data): - try: - required(['person_id', 'block'], data) - integer_expected(['post_id'], data) - boolean_expected(['block'], data) - except: - raise + required(['person_id', 'block'], data) + integer_expected(['post_id'], data) + boolean_expected(['block'], data) person_id = data['person_id'] block = data['block'] - try: - if block == True: - user_id = block_another_user(person_id, SRC_API, auth) - else: - user_id = unblock_another_user(person_id, SRC_API, auth) - user_json = user_view(user=person_id, variant=4, user_id=user_id) - return user_json - except: - raise + user_id = block_another_user(person_id, SRC_API, auth) if block else unblock_another_user(person_id, SRC_API, auth) + user_json = user_view(user=person_id, variant=4, user_id=user_id) + return user_json diff --git a/app/api/alpha/views.py b/app/api/alpha/views.py index 4e2ea483..f7fa2eb5 100644 --- a/app/api/alpha/views.py +++ b/app/api/alpha/views.py @@ -82,12 +82,9 @@ def post_view(post: Post | int, variant, stub=False, user_id=None, my_vote=0): 'saved': saved, 'read': False, 'hidden': False, 'unread_comments': post.reply_count, 'my_vote': my_vote, 'activity_alert': activity_alert, 'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin} - try: - creator = user_view(user=post.user_id, variant=1, stub=True) - community = community_view(community=post.community_id, variant=1, stub=True) - v2.update({'creator': creator, 'community': community}) - except: - raise + creator = user_view(user=post.user_id, variant=1, stub=True) + community = community_view(community=post.community_id, variant=1, stub=True) + v2.update({'creator': creator, 'community': community}) return v2 @@ -319,13 +316,10 @@ def reply_view(reply: PostReply | int, variant, user_id=None, my_vote=0): v2 = {'comment': reply_view(reply=reply, variant=1), 'counts': counts, 'banned_from_community': False, 'subscribed': 'NotSubscribed', 'saved': saved, 'creator_blocked': False, 'my_vote': my_vote, 'activity_alert': activity_alert, 'creator_banned_from_community': creator_banned_from_community, 'creator_is_moderator': creator_is_moderator, 'creator_is_admin': creator_is_admin} - try: - creator = user_view(user=reply.user_id, variant=1, stub=True) - community = community_view(community=reply.community_id, variant=1, stub=True) - post = post_view(post=reply.post_id, variant=1) - v2.update({'creator': creator, 'community': community, 'post': post}) - except: - raise + creator = user_view(user=reply.user_id, variant=1, stub=True) + community = community_view(community=reply.community_id, variant=1, stub=True) + post = post_view(post=reply.post_id, variant=1) + v2.update({'creator': creator, 'community': community, 'post': post}) return v2 diff --git a/app/shared/auth.py b/app/shared/auth.py index f159d220..b7bb4f90 100644 --- a/app/shared/auth.py +++ b/app/shared/auth.py @@ -22,11 +22,8 @@ def log_user_in(input, src): username = input.user_name.data password = input.password.data elif src == SRC_API: - try: - required(["username_or_email", "password"], input) - string_expected(["username_or_email", "password"], input) - except Exception: - raise + required(["username_or_email", "password"], input) + string_expected(["username_or_email", "password"], input) username = input['username_or_email'] password = input['password'] diff --git a/app/shared/community.py b/app/shared/community.py index 4b79b84e..a226ccb3 100644 --- a/app/shared/community.py +++ b/app/shared/community.py @@ -20,10 +20,7 @@ def join_community(community_id: int, src, auth=None, user_id=None, main_user_na community = Community.query.get(community_id) if not community: raise Exception('community_not_found') - try: - user = authorise_api_user(auth, return_type='model') - except: - raise + user = authorise_api_user(auth, return_type='model') else: community = Community.query.get_or_404(community_id) if not user_id: @@ -118,10 +115,7 @@ def leave_community(community_id: int, src, auth=None): community = Community.query.get(community_id) if not community: raise Exception('community_not_found') - try: - user = authorise_api_user(auth, return_type='model') - except: - raise + user = authorise_api_user(auth, return_type='model') else: community = Community.query.get_or_404(community_id) user = current_user @@ -183,10 +177,7 @@ def leave_community(community_id: int, src, auth=None): def block_community(community_id, src, auth=None): if src == SRC_API: - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: user_id = current_user.id @@ -204,10 +195,7 @@ def block_community(community_id, src, auth=None): def unblock_community(community_id, src, auth=None): if src == SRC_API: - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: user_id = current_user.id diff --git a/app/shared/post.py b/app/shared/post.py index c18c4de2..91cd5d3d 100644 --- a/app/shared/post.py +++ b/app/shared/post.py @@ -23,10 +23,7 @@ def vote_for_post(post_id: int, vote_direction, src, auth=None): post = Post.query.get(post_id) if not post: raise Exception('post_not_found') - try: - user = authorise_api_user(auth, return_type='model') - except: - raise + user = authorise_api_user(auth, return_type='model') else: post = Post.query.get_or_404(post_id) user = current_user @@ -103,10 +100,7 @@ def bookmark_the_post(post_id: int, src, auth=None): post = Post.query.get(post_id) if not post or post.deleted: raise Exception('post_not_found') - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: post = Post.query.get_or_404(post_id) if post.deleted: @@ -136,10 +130,7 @@ def remove_the_bookmark_from_post(post_id: int, src, auth=None): post = Post.query.get(post_id) if not post or post.deleted: raise Exception('post_not_found') - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: post = Post.query.get_or_404(post_id) if post.deleted: @@ -168,10 +159,7 @@ def toggle_post_notification(post_id: int, src, auth=None): post = Post.query.get(post_id) if not post or post.deleted: raise Exception('post_not_found') - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: post = Post.query.get_or_404(post_id) if post.deleted: diff --git a/app/shared/reply.py b/app/shared/reply.py index 77169b51..30dc92e0 100644 --- a/app/shared/reply.py +++ b/app/shared/reply.py @@ -23,10 +23,7 @@ def vote_for_reply(reply_id: int, vote_direction, src, auth=None): reply = PostReply.query.get(reply_id) if not reply: raise Exception('reply_not_found') - try: - user = authorise_api_user(auth, return_type='model') - except: - raise + user = authorise_api_user(auth, return_type='model') else: reply = PostReply.query.get_or_404(post_id) user = current_user @@ -103,10 +100,7 @@ def bookmark_the_post_reply(comment_id: int, src, auth=None): post_reply = PostReply.query.get(comment_id) if not post_reply or post_reply.deleted: raise Exception('comment_not_found') - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: post_reply = PostReply.query.get_or_404(comment_id) if post_reply.deleted: @@ -137,10 +131,7 @@ def remove_the_bookmark_from_post_reply(comment_id: int, src, auth=None): post_reply = PostReply.query.get(comment_id) if not post_reply or post_reply.deleted: raise Exception('comment_not_found') - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: post_reply = PostReply.query.get_or_404(comment_id) if post_reply.deleted: @@ -169,10 +160,7 @@ def toggle_post_reply_notification(post_reply_id: int, src, auth=None): post_reply = PostReply.query.get(post_reply_id) if not post_reply or post_reply.deleted: raise Exception('comment_not_found') - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: post_reply = PostReply.query.get_or_404(post_reply_id) if post_reply.deleted: diff --git a/app/shared/user.py b/app/shared/user.py index b5916b11..57f2a895 100644 --- a/app/shared/user.py +++ b/app/shared/user.py @@ -21,10 +21,7 @@ SRC_API = 3 def block_another_user(person_id, src, auth=None): if src == SRC_API: - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: user_id = current_user.id @@ -63,10 +60,7 @@ def block_another_user(person_id, src, auth=None): def unblock_another_user(person_id, src, auth=None): if src == SRC_API: - try: - user_id = authorise_api_user(auth) - except: - raise + user_id = authorise_api_user(auth) else: user_id = current_user.id