From 2e137d2df769637e1e78c93003f89977373d7f48 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Thu, 2 May 2024 11:01:34 -0500 Subject: [PATCH] Better exception message when database encounters a duplicate unique key --- lib/DbConnection.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/DbConnection.php b/lib/DbConnection.php index d46a163e..3a192b65 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -117,7 +117,7 @@ class DbConnection{ } 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(); + throw new Exceptions\DuplicateDatabaseKeyException(str_replace('SQLSTATE[23000]: Integrity constraint violation: 1062 ', '', $ex->getMessage() . '. Query: ' . $sql . '. Parameters: ' . vds($params))); } else{ throw $this->CreateDetailedException($ex, $sql, $params); @@ -132,12 +132,12 @@ class DbConnection{ /** * @param \PDOException $ex The exception to create details from. - * @param string $preparedSql The prepared SQL that caused the exception. + * @param string $sql The prepared SQL that caused the exception. * @param array $params The parameters passed to the prepared SQL. */ - private function CreateDetailedException(\PDOException $ex, string $preparedSql, array $params): Exceptions\DatabaseQueryException{ + private function CreateDetailedException(\PDOException $ex, string $sql, array $params): Exceptions\DatabaseQueryException{ // Throw a custom exception that includes more information on the query and paramaters - return new Exceptions\DatabaseQueryException('Error when executing query: ' . $ex->getMessage() . '. Query: ' . $preparedSql . '. Parameters: ' . vds($params)); + return new Exceptions\DatabaseQueryException('Error when executing query: ' . $ex->getMessage() . '. Query: ' . $sql . '. Parameters: ' . vds($params)); } /**