Skip to content

Conversation

@kzndotsh
Copy link
Contributor

@kzndotsh kzndotsh commented Jul 20, 2025

  • Replace old sentry.py with new sentry_manager.py
  • Update error handlers with improved context
  • Streamline sentry error reporting

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. If this change fixes any issues please put "Fixes #XX" in the description. Please also ensure to add the appropriate labels to the PR.

Guidelines

  • My code follows the style guidelines of this project (formatted with Ruff)

  • I have performed a self-review of my own code

  • I have commented my code, particularly in hard-to-understand areas

  • I have made corresponding changes to the documentation if needed

  • My changes generate no new warnings

  • I have tested this change

  • Any dependent changes have been merged and published in downstream modules

  • I have added all appropriate labels to this PR

  • I have followed all of these guidelines.

How Has This Been Tested? (if applicable)

Please describe how you tested your code. e.g describe what commands you ran, what arguments, and any config stuff (if applicable)

Screenshots (if applicable)

Please add screenshots to help explain your changes.

Additional Information

Please add any other information that is important to this PR.

Summary by Sourcery

Modernize error handling and Sentry integration by replacing legacy sentry.py logic with a centralized SentryManager, enriching error configurations with status metadata, and simplifying transaction management and context extraction.

Enhancements:

  • Introduce sentry_status attribute in ErrorHandlerConfig and map each error type to an appropriate Sentry status
  • Delegate error transaction finalization and exception capturing to the new SentryManager API
  • Refactor context building in error handlers to use a shared get_interaction_context helper for consistency
  • Simplify SentryHandler cog to set span status via SentryManager on command and interaction completion
  • Lower verbosity of command suggestion logs by switching several debug statements to trace level

Chores:

  • Remove deprecated sentry.py logic and related helper methods from error handlers

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jul 20, 2025

Reviewer's Guide

This refactor modernizes Sentry integration by migrating all Sentry operations to a centralized SentryManager, enriches error configurations with explicit status mapping, and streamlines context building and logging in the error handler while replacing the old Sentry handler Cog with a lean implementation that uses the manager.

ER diagram for ErrorHandlerConfig and Sentry status mapping

erDiagram
    ERROR_HANDLER_CONFIG {
        string message_format
        string log_level
        string sentry_status
        boolean send_to_sentry
    }
    SENTRY_MANAGER {
        dict STATUS
    }
    ERROR_HANDLER_CONFIG ||--o{ SENTRY_MANAGER : "uses STATUS for sentry_status"
Loading

Class diagram for Sentry integration refactor

classDiagram
    class SentryManager {
        +STATUS: dict
        +is_initialized: bool
        +capture_exception(error, context, level, tags)
        +finish_transaction_on_error()
        +set_command_context(source)
    }

    class SentryHandler {
        -sentry_manager: SentryManager
        +on_command_completion(ctx)
        +on_app_command_completion(interaction, command)
    }

    class ErrorHandlerConfig {
        +message_format: str
        +log_level: str
        +sentry_status: str|None
        +send_to_sentry: bool
        +detail_extractor: callable|None
    }

    SentryHandler --> SentryManager : uses
    ErrorHandlerConfig --> SentryManager : uses STATUS
Loading

Class diagram for updated error handling flow

classDiagram
    class ErrorHandler {
        +_handle_error(source, error)
        +_get_log_context(source, user, error)
        +_get_formatted_message(source, error, config)
        +_log_and_report_error(root_error, error_type, log_context, config)
        +_try_edit_message_with_sentry_id(sent_message, sentry_event_id, log_context)
    }
    ErrorHandler --> SentryManager : uses
    ErrorHandler --> ErrorHandlerConfig : uses
Loading

File-Level Changes

Change Details Files
Migrated Sentry operations in the error handler to SentryManager
  • Replaced _finish_sentry_transaction_on_error calls with bot.sentry_manager.finish_transaction_on_error()
  • Replaced internal _capture_exception_with_context with bot.sentry_manager.capture_exception(...)
  • Added bot.sentry_manager.set_command_context(source) after reporting errors
tux/handlers/error.py
Extended ErrorHandlerConfig with explicit Sentry status mapping
  • Added sentry_status field to the config class defaulting to ERROR
  • Populated sentry_status for each exception type using SentryManager.STATUS[...]
tux/handlers/error.py
Refactored error context construction and logging levels
  • Replaced verbose manual context assembly with get_interaction_context(source)
  • Simplified docstrings and parameter formatting in _get_log_context
  • Downgraded suggestion logs from DEBUG to TRACE for consistency
tux/handlers/error.py
Replaced legacy SentryHandler Cog with SentryManager-based implementation
  • Removed custom transaction creation/finish logic and inline status maps
  • Simplified listeners to set current span status to OK via sentry_manager
  • Updated setup to inject SentryManager instead of bot instance
tux/handlers/sentry.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link
Contributor

github-actions bot commented Jul 20, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@codecov
Copy link

codecov bot commented Jul 20, 2025

Codecov Report

Attention: Patch coverage is 0% with 36 lines in your changes missing coverage. Please review.

Project coverage is 9.31%. Comparing base (3e4f69e) to head (63049dc).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/handlers/error.py 0.00% 23 Missing ⚠️
tux/handlers/sentry.py 0.00% 13 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main    #958      +/-   ##
========================================
+ Coverage   9.26%   9.31%   +0.04%     
========================================
  Files        123     123              
  Lines      10390   10266     -124     
  Branches    1276    1252      -24     
========================================
- Hits         963     956       -7     
+ Misses      9325    9209     -116     
+ Partials     102     101       -1     
Flag Coverage Δ *Carryforward flag
database 0.31% <ø> (+<0.01%) ⬆️ Carriedforward from 3e4f69e
integration 5.93% <0.00%> (+0.07%) ⬆️
unit 6.39% <0.00%> (+0.07%) ⬆️

*This pull request uses carry forward flags. Click here to find out more.

Components Coverage Δ
Core Bot Infrastructure 16.45% <ø> (ø)
Database Layer 0.00% <ø> (ø)
Bot Commands & Features 0.00% <ø> (ø)
Event & Error Handling ∅ <ø> (∅)
Utilities & Helpers ∅ <ø> (∅)
User Interface Components 0.00% <ø> (ø)
CLI Interface ∅ <ø> (∅)
External Service Wrappers ∅ <ø> (∅)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kzndotsh kzndotsh force-pushed the 02-error-handling-split branch from 7179364 to 63049dc Compare July 20, 2025 04:39
- Replace old sentry.py with new sentry_manager.py
- Update error handlers with improved context
- Streamline sentry error reporting
@kzndotsh kzndotsh force-pushed the 02-error-handling-split branch from 63049dc to b25c120 Compare July 20, 2025 04:51
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 20, 2025

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: b25c120
Status: ✅  Deploy successful!
Preview URL: https://021d53ce.tux-afh.pages.dev
Branch Preview URL: https://02-error-handling-split.tux-afh.pages.dev

View logs

@kzndotsh kzndotsh closed this Jul 20, 2025
@kzndotsh kzndotsh deleted the 02-error-handling-split branch July 20, 2025 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants