From 2dada0dd80bbd73f9037cd56368b91bc31d182e6 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 23 May 2025 06:29:18 -0400 Subject: [PATCH] Refactor ModrinthTracker commands to improve error handling and response validation. Replace direct response status checks with data validation, enhance logging for exceptions, and provide clearer user feedback for search and stats commands. --- modrinthtracker/modrinthtracker.py | 35 ++++++++++++++---------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/modrinthtracker/modrinthtracker.py b/modrinthtracker/modrinthtracker.py index 12f7fbb..a10740e 100644 --- a/modrinthtracker/modrinthtracker.py +++ b/modrinthtracker/modrinthtracker.py @@ -260,12 +260,11 @@ class ModrinthTracker(commands.Cog): "limit": 5, "index": "relevance" } - response = await self._make_request(f"{BASE_URL}/search", params=params) - if response.status != 200: + data = await self._make_request(f"{BASE_URL}/search", params=params) + if not data: await ctx.send("Failed to search Modrinth projects.") return - data = await response.json() if not data["hits"]: await ctx.send("No projects found matching your query.") return @@ -292,25 +291,24 @@ class ModrinthTracker(commands.Cog): await ctx.send(embed=embed) except Exception as e: - await ctx.send(f"An error occurred while searching: {str(e)}") + self.bot.logger.error(f"Error in modrinth search command: {str(e)}", exc_info=True) + await ctx.send("An unexpected error occurred while searching. Please try again later.") @modrinth.command() async def stats(self, ctx, project_id: str): """Show detailed statistics for a tracked project.""" try: - response = await self._make_request(f"{BASE_URL}/project/{project_id}") - if response.status != 200: + # Get project info + project_data = await self._make_request(f"{BASE_URL}/project/{project_id}") + if not project_data: await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.") return - project_data = await response.json() - # Get version history - response = await self._make_request(f"{BASE_URL}/project/{project_id}/version") - if response.status != 200: + versions = await self._make_request(f"{BASE_URL}/project/{project_id}/version") + if not versions: await ctx.send("Error: Could not fetch version information.") return - versions = await response.json() embed = discord.Embed( title=f"📊 {project_data['title']} Statistics", @@ -355,6 +353,7 @@ class ModrinthTracker(commands.Cog): await ctx.send(embed=embed) except Exception as e: + self.bot.logger.error(f"Error in modrinth stats command: {str(e)}", exc_info=True) await ctx.send(f"An error occurred while fetching statistics: {str(e)}") @modrinth.command() @@ -369,20 +368,17 @@ class ModrinthTracker(commands.Cog): try: # Get project info - response = await self._make_request(f"{BASE_URL}/project/{project_id}") - if response.status != 200: + project_data = await self._make_request(f"{BASE_URL}/project/{project_id}") + if not project_data: await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.") return - project_data = await response.json() - # Get version history - response = await self._make_request(f"{BASE_URL}/project/{project_id}/version") - if response.status != 200: + versions = await self._make_request(f"{BASE_URL}/project/{project_id}/version") + if not versions: await ctx.send("Error: Could not fetch version information.") return - versions = await response.json() if not versions: await ctx.send("No version information available for this project.") return @@ -431,7 +427,8 @@ class ModrinthTracker(commands.Cog): await ctx.send(embed=embed) except Exception as e: - await ctx.send(f"An error occurred while fetching version history: {str(e)}") + self.bot.logger.error(f"Error in modrinth versions command: {str(e)}", exc_info=True) + await ctx.send("An unexpected error occurred while fetching version history. Please try again later.") async def update_checker(self): await self.bot.wait_until_ready()