Skip to content

Conversation

electron271
Copy link
Member

@electron271 electron271 commented Aug 9, 2025

Description

if db errors people get their level reset

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.

Copy link
Contributor

github-actions bot commented Aug 9, 2025

Dependency Review

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

Scanned Files

None

Copy link
Contributor

sourcery-ai bot commented Aug 9, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactor get_xp_and_level to fail fast on database errors or missing data by raising exceptions instead of returning default XP/level values, ensuring XP isn’t reset silently on errors.

Sequence diagram for error handling in get_xp_and_level

sequenceDiagram
    participant Caller
    participant LevelsController
    participant Database
    Caller->>LevelsController: get_xp_and_level(member_id, guild_id)
    LevelsController->>Database: find_one(where={member_id, guild_id})
    alt Record found and has xp/level
        LevelsController-->>Caller: return (xp, level)
    else Record missing or error occurs
        LevelsController-->>Caller: raise ValueError
    end
Loading

Class diagram for updated LevelsController error handling

classDiagram
    class LevelsController {
        +get_xp_and_level(member_id: int, guild_id: int) tuple[float, int]
        +get_last_message_time(member_id: int, guild_id: int) datetime | None
        -_fail(msg: str) NoReturn
    }
    LevelsController : get_xp_and_level() raises ValueError on error or missing data
Loading

File-Level Changes

Change Details Files
Replace default-silent error handling with explicit exceptions
  • Added internal _fail helper to raise ValueError with custom messages
  • Removed logger.error and default return in except block
  • Changed except block to call _fail on any exception
tux/database/controllers/levels.py
Tighten record validation and field extraction
  • Return (0.0, 0) immediately if no record found
  • Check xp and level attributes explicitly and call _fail if missing
  • Use getattr(...) and cast to extract xp and level instead of safe_get_attr
tux/database/controllers/levels.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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @electron271 - I've reviewed your changes - here's some feedback:

  • The docstring and signature claim it can return None for missing records, but the code always returns a tuple – update the docs or adjust the return logic to match.
  • Catching all Exceptions and rethrowing as ValueError may obscure real issues; consider catching only DB errors and/or introducing a custom exception type for clarity.
  • Defining and using a dedicated error type instead of ValueError (e.g. LevelsRetrievalError) would help callers distinguish record-not-found vs. system failures.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The docstring and signature claim it can return None for missing records, but the code always returns a tuple – update the docs or adjust the return logic to match.
- Catching all Exceptions and rethrowing as ValueError may obscure real issues; consider catching only DB errors and/or introducing a custom exception type for clarity.
- Defining and using a dedicated error type instead of ValueError (e.g. LevelsRetrievalError) would help callers distinguish record-not-found vs. system failures.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

codecov bot commented Aug 9, 2025

Codecov Report

❌ Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 9.62%. Comparing base (567b270) to head (b039243).
⚠️ Report is 4 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tux/database/controllers/levels.py 0.00% 12 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main   #1018      +/-   ##
========================================
- Coverage   9.63%   9.62%   -0.01%     
========================================
  Files        123     123              
  Lines      10414   10421       +7     
  Branches    1279    1280       +1     
========================================
  Hits        1003    1003              
- Misses      9307    9315       +8     
+ Partials     104     103       -1     
Flag Coverage Δ *Carryforward flag
database 0.31% <ø> (+<0.01%) ⬆️ Carriedforward from 67179a7
integration 5.85% <0.00%> (-0.01%) ⬇️
unit 6.30% <0.00%> (-0.01%) ⬇️

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

Components Coverage Δ
Core Bot Infrastructure 16.43% <ø> (ø)
Database Layer 0.00% <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.

Copy link

cloudflare-workers-and-pages bot commented Aug 9, 2025

Deploying tux with  Cloudflare Pages  Cloudflare Pages

Latest commit: 67179a7
Status: ✅  Deploy successful!
Preview URL: https://3b360d6b.tux-afh.pages.dev
Branch Preview URL: https://level-fix.tux-afh.pages.dev

View logs

Copy link
Collaborator

@cherryl1k cherryl1k left a comment

Choose a reason for hiding this comment

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

Looks like it does what its supposed to!

@electron271
Copy link
Member Author

giving it a final test

@electron271
Copy link
Member Author

looks good

Copy link
Collaborator

@cherryl1k cherryl1k left a comment

Choose a reason for hiding this comment

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

seems good

@electron271 electron271 merged commit e6c6452 into main Aug 9, 2025
35 checks passed
@electron271 electron271 deleted the level-fix branch August 9, 2025 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants