fix: guard roll_back against NameError when db_post fails in complete_game (#27) #59

Closed
cal wants to merge 13 commits from ai/paper-dynasty-discord-27 into next-release
Owner

Summary

  • Initializes db_game = None before the try block in complete_game
  • Guards the roll_back(db_game["id"]) call with if db_game is not None: so a failed db_post (which leaves db_game unassigned) no longer causes a secondary NameError that masks the original exception and prevents rollback

Root Cause

command_logic/logic_gameplay.py:4284–4290 — the except block referenced db_game["id"] unconditionally. If db_post("games", ...) raised before db_game was assigned, the exception handler itself would raise NameError: name 'db_game' is not defined, hiding the real error and skipping the intended rollback.

Files Changed

  • command_logic/logic_gameplay.py — 3-line targeted fix

Test Results

No test suite configured (none per issue metadata). Change verified by reading back the modified block.

Other Observations

The commit also includes a few cosmetic reformatting changes (blank-line removal, parenthesized ternaries) that were already present as pre-existing working-tree modifications on this file before my edit was applied — they are unrelated to this fix.

Closes #27

## Summary - Initializes `db_game = None` before the `try` block in `complete_game` - Guards the `roll_back(db_game["id"])` call with `if db_game is not None:` so a failed `db_post` (which leaves `db_game` unassigned) no longer causes a secondary `NameError` that masks the original exception and prevents rollback ## Root Cause `command_logic/logic_gameplay.py:4284–4290` — the `except` block referenced `db_game["id"]` unconditionally. If `db_post("games", ...)` raised before `db_game` was assigned, the exception handler itself would raise `NameError: name 'db_game' is not defined`, hiding the real error and skipping the intended rollback. ## Files Changed - `command_logic/logic_gameplay.py` — 3-line targeted fix ## Test Results No test suite configured (`none` per issue metadata). Change verified by reading back the modified block. ## Other Observations The commit also includes a few cosmetic reformatting changes (blank-line removal, parenthesized ternaries) that were already present as pre-existing working-tree modifications on this file before my edit was applied — they are unrelated to this fix. Closes #27
cal added 1 commit 2026-03-05 11:03:20 +00:00
fix: guard roll_back against NameError when db_post fails in complete_game (#27)
All checks were successful
Build Docker Image / build (pull_request) Successful in 1m13s
073d5a6241
Initialize db_game = None before the try block so the except clause can
safely check `if db_game is not None` before attempting rollback. Previously,
if db_post raised before assigning db_game, the except block would raise a
NameError, masking the original error and skipping rollback entirely.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
cal added the
ai-reviewing
label 2026-03-05 11:15:32 +00:00
cal reviewed 2026-03-05 11:16:31 +00:00
cal left a comment
Author
Owner

AI Code Review

Files Reviewed

  • command_logic/logic_gameplay.py (modified)

Findings

Correctness

  • The fix is correct and directly addresses the root cause. Initializing db_game = None before the try block and guarding roll_back(db_game["id"]) with if db_game is not None: properly prevents the secondary NameError when db_post("games", ...) fails before assignment.
  • No off-by-one errors, type mismatches, or null issues introduced.

Security

  • No issues found. No new inputs, queries, or credentials involved.

Style & Conventions

  • The cosmetic reformatting changes (parenthesized ternaries, reformatted function signatures, tuple assignment, list comprehension) all improve PEP 8 compliance and readability. No convention violations.

Suggestions

  • Out-of-scope / pre-existing issue: If db_post("games") raises, the except block runs and execution continues to line 4294 (db_post("plays", payload=db_ready_plays)), where db_ready_plays is also undefined (assigned inside the same failed try block). This would raise a NameError there too. Consider adding an early return after the rollback in the first except block to halt the function cleanly — but this is a separate concern from issue #27 and need not block this merge.

Verdict: APPROVED

Clean, targeted fix. The db_game = None sentinel + null guard is the standard Python idiom for this pattern and correctly resolves the masked exception / skipped rollback bug. The accompanying reformatting changes are harmless readability improvements.


Automated review by Claude PR Reviewer (posted as COMMENT — Gitea blocks self-approval)

## AI Code Review ### Files Reviewed - `command_logic/logic_gameplay.py` (modified) ### Findings #### Correctness - The fix is correct and directly addresses the root cause. Initializing `db_game = None` before the `try` block and guarding `roll_back(db_game["id"])` with `if db_game is not None:` properly prevents the secondary `NameError` when `db_post("games", ...)` fails before assignment. - No off-by-one errors, type mismatches, or null issues introduced. #### Security - No issues found. No new inputs, queries, or credentials involved. #### Style & Conventions - The cosmetic reformatting changes (parenthesized ternaries, reformatted function signatures, tuple assignment, list comprehension) all improve PEP 8 compliance and readability. No convention violations. #### Suggestions - **Out-of-scope / pre-existing issue**: If `db_post("games")` raises, the `except` block runs and execution continues to line 4294 (`db_post("plays", payload=db_ready_plays)`), where `db_ready_plays` is also undefined (assigned inside the same failed `try` block). This would raise a `NameError` there too. Consider adding an early `return` after the rollback in the first `except` block to halt the function cleanly — but this is a separate concern from issue #27 and need not block this merge. ### Verdict: APPROVED Clean, targeted fix. The `db_game = None` sentinel + null guard is the standard Python idiom for this pattern and correctly resolves the masked exception / skipped rollback bug. The accompanying reformatting changes are harmless readability improvements. --- *Automated review by Claude PR Reviewer (posted as COMMENT — Gitea blocks self-approval)*
cal added
ai-reviewed
and removed
ai-reviewing
labels 2026-03-05 11:17:04 +00:00
cal changed target branch from main to next-release 2026-03-07 07:32:32 +00:00
cal force-pushed ai/paper-dynasty-discord-27 from 073d5a6241 to afc76db241 2026-03-07 07:33:39 +00:00 Compare
cal closed this pull request 2026-03-10 14:10:22 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: cal/paper-dynasty-discord#59
No description provided.