Use new duplicate key exception instead of checking PDOException

This commit is contained in:
Alex Cabal 2024-04-29 11:50:52 -05:00
parent 5ee7e8e2ad
commit 49f5bc7641
4 changed files with 14 additions and 29 deletions

View file

@ -1,4 +1,8 @@
<? <?
use Safe\DateTimeImmutable;
use function Safe\preg_match;
use function Safe\posix_getpwuid;
class DbConnection{ class DbConnection{
private ?\PDO $_link = null; private ?\PDO $_link = null;
public int $QueryCount = 0; public int $QueryCount = 0;
@ -104,17 +108,16 @@ class DbConnection{
$done = true; $done = true;
} }
catch(\PDOException $ex){ catch(\PDOException $ex){
if(isset($ex->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. // InnoDB deadlock, this is normal and happens occasionally. All we have to do is retry the query.
$deadlockRetries++; $deadlockRetries++;
usleep(500000 * $deadlockRetries); // Give the deadlock some time to clear up. Start at .5 seconds 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 // Duplicate key, bubble this up without logging it so the business logic can handle it
throw new Exceptions\DuplicateDatabaseKeyException(); throw new Exceptions\DuplicateDatabaseKeyException();
} }
else{ else{
$done = true;
throw $this->CreateDetailedException($ex, $sql, $params); throw $this->CreateDetailedException($ex, $sql, $params);
} }
} }
@ -138,7 +141,7 @@ class DbConnection{
/** /**
* @return Array<mixed> * @return Array<mixed>
*/ */
private function ExecuteQuery(PDOStatement $handle, string $class = 'stdClass'): array{ private function ExecuteQuery(\PDOStatement $handle, string $class = 'stdClass'): array{
$handle->execute(); $handle->execute();
$this->LastQueryAffectedRowCount = $handle->rowCount(); $this->LastQueryAffectedRowCount = $handle->rowCount();
@ -248,7 +251,7 @@ class DbConnection{
catch(\PDOException $ex){ catch(\PDOException $ex){
// HY000 is thrown when there is no result set, e.g. for an update operation. // 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 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; throw $ex;
} }
} }

View file

@ -56,14 +56,8 @@ class NewsletterSubscription extends Accessor{
?) ?)
', [$this->User->UserId, false, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->Created]); ', [$this->User->UserId, false, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->Created]);
} }
catch(PDOException $ex){ catch(Exceptions\DuplicateDatabaseKeyException){
if(($ex->errorInfo[1] ?? 0) == 1062){ throw new Exceptions\NewsletterSubscriptionExistsException();
// Duplicate unique key; email already in use
throw new Exceptions\NewsletterSubscriptionExistsException();
}
else{
throw $ex;
}
} }
// Send the double opt-in confirmation email // Send the double opt-in confirmation email

View file

@ -63,14 +63,8 @@ class Payment extends Accessor{
?) ?)
', [$this->UserId, $this->Created, $this->ChannelId, $this->TransactionId, $this->Amount, $this->Fee, $this->IsRecurring, $this->IsMatchingDonation]); ', [$this->UserId, $this->Created, $this->ChannelId, $this->TransactionId, $this->Amount, $this->Fee, $this->IsRecurring, $this->IsMatchingDonation]);
} }
catch(PDOException $ex){ catch(Exceptions\DuplicateDatabaseKeyException){
if(($ex->errorInfo[1] ?? 0) == 1062){ throw new Exceptions\PaymentExistsException();
// Duplicate unique key; transction ID already exists
throw new Exceptions\PaymentExistsException();
}
else{
throw $ex;
}
} }
$this->PaymentId = Db::GetLastInsertedId(); $this->PaymentId = Db::GetLastInsertedId();

View file

@ -95,14 +95,8 @@ class User extends Accessor{
?) ?)
', [$this->Email, $this->Name, $this->Uuid, $this->Created, $this->PasswordHash]); ', [$this->Email, $this->Name, $this->Uuid, $this->Created, $this->PasswordHash]);
} }
catch(PDOException $ex){ catch(Exceptions\DuplicateDatabaseKeyException){
if(($ex->errorInfo[1] ?? 0) == 1062){ throw new Exceptions\UserExistsException();
// Duplicate unique key; email already in use
throw new Exceptions\UserExistsException();
}
else{
throw $ex;
}
} }
$this->UserId = Db::GetLastInsertedId(); $this->UserId = Db::GetLastInsertedId();