From 8013be2a6625182c2a7c8bd1252a405fa70c79b1 Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Fri, 15 Nov 2024 01:04:53 +0000 Subject: [PATCH] fix: Add error handling to all cogs' initialization - Add proper logging - Add teardown functions - Add cleanup on failure --- birthday/__init__.py | 26 ++++++++++++++++++++++---- overseerr/__init__.py | 19 +++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/birthday/__init__.py b/birthday/__init__.py index 49a0b85..1741913 100644 --- a/birthday/__init__.py +++ b/birthday/__init__.py @@ -1,13 +1,31 @@ """Birthday cog for Red-DiscordBot""" from redbot.core.bot import Red +import logging from .birthday import Birthday +logger = logging.getLogger("Birthday") + async def setup(bot: Red) -> None: """Load Birthday cog.""" - cog = Birthday(bot) - await bot.add_cog(cog) + try: + cog = Birthday(bot) + await bot.add_cog(cog) + # Initialize scheduled tasks + try: + await cog.reload_scheduled_tasks() + except Exception as e: + logger.error(f"Failed to initialize scheduled tasks: {str(e)}") + await bot.remove_cog(cog.__class__.__name__) + raise + except Exception as e: + logger.error(f"Failed to load Birthday cog: {str(e)}") + raise async def teardown(bot: Red) -> None: """Clean up when unloading.""" - # Let Red handle command cleanup - pass + try: + if "Birthday" in bot.cogs: + await bot.remove_cog("Birthday") + except Exception as e: + logger.error(f"Error during teardown: {str(e)}") + raise diff --git a/overseerr/__init__.py b/overseerr/__init__.py index 371371e..9b28596 100644 --- a/overseerr/__init__.py +++ b/overseerr/__init__.py @@ -1,13 +1,24 @@ """Overseerr cog for Red-DiscordBot""" from redbot.core.bot import Red +import logging from .overseerr import Overseerr +logger = logging.getLogger("Overseerr") + async def setup(bot: Red) -> None: """Load Overseerr cog.""" - cog = Overseerr(bot) - await bot.add_cog(cog) + try: + cog = Overseerr(bot) + await bot.add_cog(cog) + except Exception as e: + logger.error(f"Failed to load Overseerr cog: {str(e)}") + raise async def teardown(bot: Red) -> None: """Clean up when unloading.""" - # Let Red handle command cleanup - pass + try: + if "Overseerr" in bot.cogs: + await bot.remove_cog("Overseerr") + except Exception as e: + logger.error(f"Error during teardown: {str(e)}") + raise