From 1cb7fdd5b300a19427e64db9b87ccee8dd9ec89f Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 1 Nov 2023 19:18:45 -0700 Subject: [PATCH] ruff: Fix TRY301 Abstract `raise` to an inner function. Signed-off-by: Anders Kaseorg --- zulip_bots/zulip_bots/bot_shell.py | 7 ++---- .../zulip_bots/bots/idonethis/idonethis.py | 23 ++++++++----------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/zulip_bots/zulip_bots/bot_shell.py b/zulip_bots/zulip_bots/bot_shell.py index 5500bd41..a13600bd 100755 --- a/zulip_bots/zulip_bots/bot_shell.py +++ b/zulip_bots/zulip_bots/bot_shell.py @@ -45,11 +45,8 @@ def main() -> None: bot_dir = os.path.dirname(bot_path) sys.path.insert(0, bot_dir) - try: - lib_module = import_module_from_source(bot_path.as_posix(), bot_name) - if lib_module is None: - raise OSError - except OSError: + lib_module = import_module_from_source(bot_path.as_posix(), bot_name) + if lib_module is None: print(f"Could not find and import bot '{bot_name}'") sys.exit(1) diff --git a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py index 12bf60c0..f80621d6 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py +++ b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py @@ -21,11 +21,6 @@ class TeamNotFoundError(Exception): self.team = team -class UnknownCommandSyntaxError(Exception): - def __init__(self, detail: str) -> None: - self.detail = detail - - class UnspecifiedProblemError(Exception): pass @@ -114,6 +109,13 @@ def entries_list(team_name: str) -> str: return response +def unknown_command_reply(detail: str) -> str: + return ( + "Sorry, I don't understand what your trying to say. Use `@mention help` to see my help. " + + detail + ) + + def create_entry(message: str) -> str: single_word_regex = re.compile("--team=([a-zA-Z0-9_]*)") multiword_regex = re.compile('"--team=([^"]*)"') @@ -133,7 +135,7 @@ def create_entry(message: str) -> str: team = default_team new_message = message else: - raise UnknownCommandSyntaxError( + return unknown_command_reply( """I don't know which team you meant for me to create an entry under. Either set a default team or pass the `--team` flag. More information in my help""" @@ -219,7 +221,7 @@ Below are some of the commands you can use, and what they do. if len(message_content) > 2: reply = team_info(" ".join(message_content[2:])) else: - raise UnknownCommandSyntaxError( + reply = unknown_command_reply( "You must specify the team in which you request information from." ) elif command in ["entries list", "list entries"]: @@ -229,7 +231,7 @@ Below are some of the commands you can use, and what they do. elif command in ["help"]: reply = self.usage() else: - raise UnknownCommandSyntaxError( + reply = unknown_command_reply( "I can't understand the command you sent me :confused: " ) except TeamNotFoundError as e: @@ -239,11 +241,6 @@ Below are some of the commands you can use, and what they do. except AuthenticationError: reply = "I can't currently authenticate with idonethis. " reply += "Can you check that your API key is correct? For more information see my documentation." - except UnknownCommandSyntaxError as e: - reply = ( - "Sorry, I don't understand what your trying to say. Use `@mention help` to see my help. " - + e.detail - ) except Exception: # catches UnspecifiedProblemException, and other problems reply = "Oh dear, I'm having problems processing your request right now. Perhaps you could try again later :grinning:" logging.exception("Exception caught")