From 49f5bc764112edf9dd0784f4668c2f520a90a3b8 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Mon, 29 Apr 2024 11:50:52 -0500 Subject: [PATCH] Use new duplicate key exception instead of checking PDOException --- lib/DbConnection.php | 13 ++++++++----- lib/NewsletterSubscription.php | 10 ++-------- lib/Payment.php | 10 ++-------- lib/User.php | 10 ++-------- 4 files changed, 14 insertions(+), 29 deletions(-) diff --git a/lib/DbConnection.php b/lib/DbConnection.php index 747bfa12..8832c46d 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -1,4 +1,8 @@ errorInfo) && $ex->errorInfo[1] == 1213 && $deadlockRetries < 3){ + if(isset($ex->errorInfo[1]) && $ex->errorInfo[1] == 1213 && $deadlockRetries < 3){ // InnoDB deadlock, this is normal and happens occasionally. All we have to do is retry the query. $deadlockRetries++; usleep(500000 * $deadlockRetries); // Give the deadlock some time to clear up. Start at .5 seconds } - elseif(isset($ex->errorInfo) && $ex->errorInfo[1] == 1062){ + elseif(isset($ex->errorInfo[1]) && $ex->errorInfo[1] == 1062){ // Duplicate key, bubble this up without logging it so the business logic can handle it throw new Exceptions\DuplicateDatabaseKeyException(); } else{ - $done = true; throw $this->CreateDetailedException($ex, $sql, $params); } } @@ -138,7 +141,7 @@ class DbConnection{ /** * @return Array */ - private function ExecuteQuery(PDOStatement $handle, string $class = 'stdClass'): array{ + private function ExecuteQuery(\PDOStatement $handle, string $class = 'stdClass'): array{ $handle->execute(); $this->LastQueryAffectedRowCount = $handle->rowCount(); @@ -248,7 +251,7 @@ class DbConnection{ catch(\PDOException $ex){ // HY000 is thrown when there is no result set, e.g. for an update operation. // If anything besides that is thrown, then send it up the stack - if(!isset($ex->errorInfo) || $ex->errorInfo[0] != "HY000"){ + if(!isset($ex->errorInfo[0]) || $ex->errorInfo[0] != "HY000"){ throw $ex; } } diff --git a/lib/NewsletterSubscription.php b/lib/NewsletterSubscription.php index 4300f978..1e414c8f 100644 --- a/lib/NewsletterSubscription.php +++ b/lib/NewsletterSubscription.php @@ -56,14 +56,8 @@ class NewsletterSubscription extends Accessor{ ?) ', [$this->User->UserId, false, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->Created]); } - catch(PDOException $ex){ - if(($ex->errorInfo[1] ?? 0) == 1062){ - // Duplicate unique key; email already in use - throw new Exceptions\NewsletterSubscriptionExistsException(); - } - else{ - throw $ex; - } + catch(Exceptions\DuplicateDatabaseKeyException){ + throw new Exceptions\NewsletterSubscriptionExistsException(); } // Send the double opt-in confirmation email diff --git a/lib/Payment.php b/lib/Payment.php index bc70ab42..b47c2ea3 100644 --- a/lib/Payment.php +++ b/lib/Payment.php @@ -63,14 +63,8 @@ class Payment extends Accessor{ ?) ', [$this->UserId, $this->Created, $this->ChannelId, $this->TransactionId, $this->Amount, $this->Fee, $this->IsRecurring, $this->IsMatchingDonation]); } - catch(PDOException $ex){ - if(($ex->errorInfo[1] ?? 0) == 1062){ - // Duplicate unique key; transction ID already exists - throw new Exceptions\PaymentExistsException(); - } - else{ - throw $ex; - } + catch(Exceptions\DuplicateDatabaseKeyException){ + throw new Exceptions\PaymentExistsException(); } $this->PaymentId = Db::GetLastInsertedId(); diff --git a/lib/User.php b/lib/User.php index 7c993a50..9a0e59b2 100644 --- a/lib/User.php +++ b/lib/User.php @@ -95,14 +95,8 @@ class User extends Accessor{ ?) ', [$this->Email, $this->Name, $this->Uuid, $this->Created, $this->PasswordHash]); } - catch(PDOException $ex){ - if(($ex->errorInfo[1] ?? 0) == 1062){ - // Duplicate unique key; email already in use - throw new Exceptions\UserExistsException(); - } - else{ - throw $ex; - } + catch(Exceptions\DuplicateDatabaseKeyException){ + throw new Exceptions\UserExistsException(); } $this->UserId = Db::GetLastInsertedId();