Skip to content

Feat log report content when report fails#371

Open
Matsukami7 wants to merge 2 commits intomainfrom
feat-log-report-content-when-report-fails
Open

Feat log report content when report fails#371
Matsukami7 wants to merge 2 commits intomainfrom
feat-log-report-content-when-report-fails

Conversation

@Matsukami7
Copy link
Contributor

@Matsukami7 Matsukami7 commented Jan 31, 2026

Initial Checklist

  • Has @tigattack been added as a reviewer?
  • If applicable, have the relevant project(s), milestone(s), and label(s) been applied?
  • If applicable, have you added details of the cog to the readme as per README.md?

Details

Does this resolve an issue?
Resolves #370

Changes

Features / Fixes

  1. Added _log_report_send_failure() helper method (~45 lines)
    • Logs failed report sends with full context (reporter, guild, channel, log channel, error)
    • Sends detailed notification to bot owners including the report content
    • Handles message truncation to fit within Discord's 2000 character limit
    • Includes proper error handling for owner notifications
  2. Added _log_report_failure() helper method (~50 lines)
    • Logs failed reports from command error handlers
    • Extracts and logs report content when available
    • Sends comprehensive error notifications to bot owners
    • Handles cases where report content is unavailable
  3. Updated on_cmd_report_error() method
    • Added report content extraction logic for unexpected errors
    • Attempts to extract from ctx.kwargs (for hybrid commands) or message content
    • Calls _log_report_failure() to preserve report content
    • Handles extraction failures gracefully
  4. Updated on_cmd_emergency_error() method
    • Same enhancements as on_cmd_report_error() for emergency reports
    • Ensures emergency reports are never lost
  5. Enhanced do_report() method
    • Wrapped log channel send operations in try-except block
    • Calls _log_report_send_failure() when sending fails
    • Re-raises exception so error handlers can process it
    • Preserves report content even when delivery fails

Breaking Changes

  • Adds breaking change which causes <issue>.

Additional

Any further notes or comments you want to make.

1. Added _log_report_send_failure() helper method (~45 lines)
   - Logs failed report sends with full context (reporter, guild, channel, log channel, error)
   - Sends detailed notification to bot owners including the report content
   - Handles message truncation to fit within Discord's 2000 character limit
   - Includes proper error handling for owner notifications
2. Added _log_report_failure() helper method (~50 lines)
   - Logs failed reports from command error handlers
   - Extracts and logs report content when available
   - Sends comprehensive error notifications to bot owners
   - Handles cases where report content is unavailable
3. Updated on_cmd_report_error() method
   - Added report content extraction logic for unexpected errors
   - Attempts to extract from ctx.kwargs (for hybrid commands) or message content
   - Calls _log_report_failure() to preserve report content
   - Handles extraction failures gracefully
4. Updated on_cmd_emergency_error() method
   - Same enhancements as on_cmd_report_error() for emergency reports
   - Ensures emergency reports are never lost
5. Enhanced do_report() method
   - Wrapped log channel send operations in try-except block
   - Calls _log_report_send_failure() when sending fails
   - Re-raises exception so error handlers can process it
   - Preserves report content even when delivery fails
@Matsukami7 Matsukami7 requested a review from tigattack January 31, 2026 22:26
@Matsukami7 Matsukami7 added enhancement Cog enhancements. cog-report Issues related to the report cog. labels Jan 31, 2026
except Exception as e:
logger.error(f"Failed to send error to owners: {e}")
# For hybrid commands, the message parameter is passed as a keyword argument
if hasattr(ctx, "kwargs") and "message" in ctx.kwargs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questions

  • is this necessary? Is ctx ever not a guild ctx? Is specific handling needed here?
  • why are there different control flows for the different types. Is the message content different?

except Exception as e:
logger.error(f"Failed to send error to owners: {e}")
# For hybrid commands, the message parameter is passed as a keyword argument
if hasattr(ctx, "kwargs") and "message" in ctx.kwargs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's best to not directly check and add it to the try except

await self._send_cmd_error_response(ctx, error, f"You are on cooldown. Try again in <t:{retry_timestamp}:R>")
return

# Unexpected error - extract and log report content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as lines 174-188. Should ideally be in a function or moved into _log_report_failure

await self.do_report(ctx.channel, ctx.message, message, False, ctx.interaction)

@cmd_report.error
async def on_cmd_report_error(self, ctx: commands.GuildContext, error):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this isnt always a guildContext this typehint is wrong.

relates to: https://github.com/rHomelab/LabBot-Cogs/pull/371/changes#r2765945867

else:
# Not an emergency or not a text-like channel (maybe can't retrieve members), just send the embed
await log_channel.send(embed=embed)
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pointless. This error already propagates up and is caught by the error handlers already

except discord.Forbidden:
logger.error("Failed to send config error notification to guild owner - no DM permissions")

async def _log_report_send_failure( # noqa: PLR0913
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.error("Report content: <unavailable>")

# Prepare message for bot owners
base_msg = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a confusing implementation. Ideally construct the entire message then batch it out instead of crazy logic to truncate.

line 566-588


# Send to bot owners
try:
await ctx.bot.send_to_owners(base_msg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this function should try to log to the log_channel first then fall back to sending to the owners

report_body = report_body[:EMBED_FIELD_VALUE_LIMIT]
logger.info(f"Report content truncated from {original_length} to {len(report_body)} characters")

log_channel = await self.get_log_channel(channel.guild)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no log channel is set, the report is also not logged. This should raise an error instead of returning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cog-report Issues related to the report cog. enhancement Cog enhancements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] log report content when report fails

2 participants