Inconsistent Error Handling #9

Open
opened 2026-02-02 15:11:23 -05:00 by jared · 0 comments
Owner

Some commands have try-except, others don't. Some use exc_info=True, others don't.

Fix: Create error handling decorator:

from functools import wraps

def handle_command_errors(func):
@wraps(func)
async def wrapper(interaction: discord.Interaction, *args, **kwargs):
try:
return await func(interaction, *args, **kwargs)
except Exception as e:
logger.error(f"Error in {func.name}: {e}", exc_info=True)
error_msg = "An unexpected error occurred. Please try again later."
if not interaction.response.is_done():
await interaction.response.send_message(error_msg, ephemeral=True)
else:
await interaction.followup.send(error_msg, ephemeral=True)
return wrapper

Usage:

@client.tree.command(name="example")
@handle_command_errors
async def example(interaction: discord.Interaction):
# Your code here

Some commands have try-except, others don't. Some use exc_info=True, others don't. Fix: Create error handling decorator: from functools import wraps def handle_command_errors(func): @wraps(func) async def wrapper(interaction: discord.Interaction, *args, **kwargs): try: return await func(interaction, *args, **kwargs) except Exception as e: logger.error(f"Error in {func.__name__}: {e}", exc_info=True) error_msg = "An unexpected error occurred. Please try again later." if not interaction.response.is_done(): await interaction.response.send_message(error_msg, ephemeral=True) else: await interaction.followup.send(error_msg, ephemeral=True) return wrapper # Usage: @client.tree.command(name="example") @handle_command_errors async def example(interaction: discord.Interaction): # Your code here
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/discordBot#9