Inconsistent Error Handling #9
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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