From 20745fc979267d4f27ee6e97290261332806993c Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 30 Aug 2024 19:01:33 +0900 Subject: [PATCH] Add Truncate Error class --- src/Database/Adapter/MariaDB.php | 3 ++- src/Database/Adapter/MySQL.php | 3 ++- src/Database/Adapter/Postgres.php | 3 ++- src/Database/Exception/Truncate.php | 9 +++++++++ tests/e2e/Adapter/Base.php | 7 +++---- 5 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 src/Database/Exception/Truncate.php diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index d52bd788c..aaaa260fe 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -10,6 +10,7 @@ use Utopia\Database\Exception as DatabaseException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Timeout as TimeoutException; +use Utopia\Database\Exception\Truncate as TruncateException; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; @@ -2247,7 +2248,7 @@ protected function processException(PDOException $e): void // Data is too big for column resize if (($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) || ($e->getCode() === '01000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1265)) { - throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e); + throw new TruncateException('Resize would result in data truncation', $e->getCode(), $e); } // Duplicate index diff --git a/src/Database/Adapter/MySQL.php b/src/Database/Adapter/MySQL.php index 5973c92a3..aaf258e30 100644 --- a/src/Database/Adapter/MySQL.php +++ b/src/Database/Adapter/MySQL.php @@ -7,6 +7,7 @@ use Utopia\Database\Exception as DatabaseException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Timeout as TimeoutException; +use Utopia\Database\Exception\Truncate as TruncateException; class MySQL extends MariaDB { @@ -112,7 +113,7 @@ protected function processException(PDOException $e): void // Data is too big for column resize if (($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) || ($e->getCode() === '01000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1265)) { - throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e); + throw new TruncateException('Resize would result in data truncation', $e->getCode(), $e); } throw $e; diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 812a1047c..8c25731d6 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -11,6 +11,7 @@ use Utopia\Database\Exception as DatabaseException; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Timeout; +use Utopia\Database\Exception\Truncate as TruncateException; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; @@ -2218,7 +2219,7 @@ protected function processException(PDOException $e): void // Data is too big for column resize if ($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 7) { - throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e); + throw new TruncateException('Resize would result in data truncation', $e->getCode(), $e); } throw $e; diff --git a/src/Database/Exception/Truncate.php b/src/Database/Exception/Truncate.php new file mode 100644 index 000000000..9bd0ffb12 --- /dev/null +++ b/src/Database/Exception/Truncate.php @@ -0,0 +1,9 @@ +updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, 128, true); $this->fail('Succeeded updating attribute size to smaller size with data that is too big'); - } catch (DatabaseException $e) { - $this->assertEquals('Resize would result in data truncation', $e->getMessage()); + } catch (TruncateException $e) { } // Test going down in size when data isn't too big. @@ -5972,8 +5972,7 @@ public function testUpdateAttributeSize(): void try { static::getDatabase()->updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, 128, true); $this->fail('Succeeded updating attribute size to smaller size with data that is too big'); - } catch (DatabaseException $e) { - $this->assertEquals('Resize would result in data truncation', $e->getMessage()); + } catch (TruncateException $e) { } }