Skip to content

fix: enhance error handling during statement execution#821

Merged
abnegate merged 1 commit intomainfrom
feat-sum-and-count-exceptions
Feb 24, 2026
Merged

fix: enhance error handling during statement execution#821
abnegate merged 1 commit intomainfrom
feat-sum-and-count-exceptions

Conversation

@eldadfux
Copy link
Member

@eldadfux eldadfux commented Feb 23, 2026

Wrap the execute method in a try-catch block to process PDOExceptions, improving error management in SQL adapter operations.

Summary by CodeRabbit

  • Refactor
    • Enhanced error handling in core database operations to improve system stability and reliability.

Wrap the execute method in a try-catch block to process PDOExceptions, improving error management in SQL adapter operations.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 23, 2026

📝 Walkthrough

Walkthrough

The pull request adds exception handling to two database execution methods in the SQL adapter. PDOException instances caught during execute operations in the count() and sum() methods are now converted to domain-specific exceptions via the processException() method for consistent error handling.

Changes

Cohort / File(s) Summary
Exception Handling
src/Database/Adapter/SQL.php
Added try/catch blocks around execute calls in count() and sum() methods to convert PDOException into domain-specific exceptions via processException().

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through database rows,
Now catching errors as it goes,
With try and catch in place so fine,
Each exception turns divine,
Exceptions wrapped, the code now glows! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding try-catch blocks to enhance error handling during PDO statement execution in the SQL adapter.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-sum-and-count-exceptions

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Database/Adapter/SQL.php (1)

3213-3216: ⚠️ Potential issue | 🟡 Minor

@throws PDOException annotation becomes stale after this change.

Once processException() intercepts all PDOExceptions (including after the recommended scope expansion above), raw PDOExceptions will no longer escape these methods. The @throws PDOException annotation should be replaced with the actual domain exceptions that processException() can produce — e.g., @throws TimeoutException, @throws DatabaseException.

The same applies to the sum() docblock at Lines 3298–3301.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Database/Adapter/SQL.php` around lines 3213 - 3216, Update the method
docblock that currently lists `@throws` PDOException (and the sum() docblock) to
remove PDOException and instead declare the concrete domain exceptions that
processException() can throw (e.g., TimeoutException, DatabaseException or
whatever specific exception classes returned by processException()). Locate the
docblocks near the methods referenced (the one ending at the shown docblock and
the sum() docblock) and replace `@throws` PDOException with the exact exception
types produced by processException(), ensuring the annotations match
processException()'s implementation and any expanded scope changes.
🧹 Nitpick comments (2)
src/Database/Adapter/SQL.php (2)

3356-3374: Same partial-coverage issue as count()prepare() and bindValue() are outside the try-catch.

$this->getPDO()->prepare($sql) (Line 3356) and the bindValue() loop (Lines 3358–3360) are unprotected and will propagate raw PDOExceptions, bypassing processException(). Apply the same scope expansion as suggested for count().

♻️ Proposed fix — expand try-catch scope in sum()
-        $stmt = $this->getPDO()->prepare($sql);
-
-        foreach ($binds as $key => $value) {
-            $stmt->bindValue($key, $value, $this->getPDOType($value));
-        }
-
         try {
+            $stmt = $this->getPDO()->prepare($sql);
+
+            foreach ($binds as $key => $value) {
+                $stmt->bindValue($key, $value, $this->getPDOType($value));
+            }
+
             $this->execute($stmt);
         } catch (PDOException $e) {
             throw $this->processException($e);
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Database/Adapter/SQL.php` around lines 3356 - 3374, The sum() method
currently calls $this->getPDO()->prepare($sql) and loops over
$stmt->bindValue(...) outside the try-catch so raw PDOExceptions bypass
processException(); move the prepare() call and the foreach bindValue loop into
the same try block that calls $this->execute($stmt) and catch PDOException to
rethrow $this->processException($e) (i.e., expand the try-catch scope in sum()
to include getPDO()->prepare($sql), the bindValue loop, and the execute($stmt)
call).

3270-3288: Extend try-catch scope to cover prepare() and bindValue() — inconsistent with find().

The new try-catch only guards execute(), leaving $this->getPDO()->prepare($sql) (Line 3270) and the bindValue() loop (Lines 3272–3274) unprotected. A PDOException thrown from either of those calls will bypass processException() and propagate as a raw PDOException, contradicting the intent of this PR.

find() (Lines 3152–3165) wraps all three — prepare(), bindValue(), and execute() — in a single try-catch block. count() and sum() should match that pattern.

♻️ Proposed fix — expand try-catch scope in count()
-        $stmt = $this->getPDO()->prepare($sql);
-
-        foreach ($binds as $key => $value) {
-            $stmt->bindValue($key, $value, $this->getPDOType($value));
-        }
-
         try {
+            $stmt = $this->getPDO()->prepare($sql);
+
+            foreach ($binds as $key => $value) {
+                $stmt->bindValue($key, $value, $this->getPDOType($value));
+            }
+
             $this->execute($stmt);
         } catch (PDOException $e) {
             throw $this->processException($e);
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Database/Adapter/SQL.php` around lines 3270 - 3288, The try-catch in
count() currently only wraps execute() so prepare() and the bindValue() loop can
throw raw PDOExceptions; expand the try-catch to encompass the call to
$this->getPDO()->prepare($sql), the foreach binding loop (where
$stmt->bindValue(...) is called), and $this->execute($stmt) so any PDOException
is routed through $this->processException($e) (mirroring the pattern used in
find()); update the block around prepare/bind/execute in count() (and similarly
in sum() if present) to catch PDOException and rethrow
$this->processException($e).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/Database/Adapter/SQL.php`:
- Around line 3213-3216: Update the method docblock that currently lists `@throws`
PDOException (and the sum() docblock) to remove PDOException and instead declare
the concrete domain exceptions that processException() can throw (e.g.,
TimeoutException, DatabaseException or whatever specific exception classes
returned by processException()). Locate the docblocks near the methods
referenced (the one ending at the shown docblock and the sum() docblock) and
replace `@throws` PDOException with the exact exception types produced by
processException(), ensuring the annotations match processException()'s
implementation and any expanded scope changes.

---

Nitpick comments:
In `@src/Database/Adapter/SQL.php`:
- Around line 3356-3374: The sum() method currently calls
$this->getPDO()->prepare($sql) and loops over $stmt->bindValue(...) outside the
try-catch so raw PDOExceptions bypass processException(); move the prepare()
call and the foreach bindValue loop into the same try block that calls
$this->execute($stmt) and catch PDOException to rethrow
$this->processException($e) (i.e., expand the try-catch scope in sum() to
include getPDO()->prepare($sql), the bindValue loop, and the execute($stmt)
call).
- Around line 3270-3288: The try-catch in count() currently only wraps execute()
so prepare() and the bindValue() loop can throw raw PDOExceptions; expand the
try-catch to encompass the call to $this->getPDO()->prepare($sql), the foreach
binding loop (where $stmt->bindValue(...) is called), and $this->execute($stmt)
so any PDOException is routed through $this->processException($e) (mirroring the
pattern used in find()); update the block around prepare/bind/execute in count()
(and similarly in sum() if present) to catch PDOException and rethrow
$this->processException($e).

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 396aea4 and 376f588.

📒 Files selected for processing (1)
  • src/Database/Adapter/SQL.php

@abnegate abnegate merged commit ba1ee9c into main Feb 24, 2026
18 checks passed
@abnegate abnegate deleted the feat-sum-and-count-exceptions branch February 24, 2026 00:37
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