From 6b53337a23be7a29c8faafe248f1600a20d2ee04 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 11 Jun 2025 16:44:32 +0530 Subject: [PATCH] update: remove duplicate exceptions from global handler. --- app/controllers/general.php | 7 ------- .../Http/Databases/Collections/Action.php | 10 ++++++++++ .../Collections/Attributes/Action.php | 19 +++++++++++-------- .../Http/Databases/Collections/Create.php | 19 +++++++++++++++++++ .../Collections/Documents/Create.php | 7 +++++-- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 0d0a253661..6602198c0b 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -1177,13 +1177,6 @@ App::error() case 'Utopia\Database\Exception\Timeout': $error = new AppwriteException(AppwriteException::DATABASE_TIMEOUT, previous: $error); break; - case 'Utopia\Database\Exception\Duplicate': - $error = new AppwriteException( - $isTablesAPI - ? AppwriteException::ROW_ALREADY_EXISTS - : AppwriteException::DOCUMENT_ALREADY_EXISTS - ); - // no break case 'Utopia\Database\Exception\Authorization': $error = new AppwriteException(AppwriteException::USER_UNAUTHORIZED); break; diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Action.php index abec90f1a6..1028f4ea7f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Action.php @@ -73,6 +73,16 @@ abstract class Action extends UtopiaAction : Exception::TABLE_ALREADY_EXISTS; } + /** + * Get the appropriate index invalid exception. + */ + final protected function getInvalidIndexException(): string + { + return $this->isCollectionsAPI() + ? Exception::INDEX_INVALID + : Exception::COLUMN_INDEX_INVALID; + } + /** * Get the exception to throw when the resource is not found. */ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php index 3939a0370c..088490ad02 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php @@ -13,7 +13,6 @@ use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Index as IndexException; use Utopia\Database\Exception\Limit as LimitException; -use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Exception\Relationship as RelationshipException; use Utopia\Database\Exception\Structure as StructureException; use Utopia\Database\Exception\Truncate as TruncateException; @@ -592,14 +591,14 @@ abstract class Action extends UtopiaAction formatOptions: $options, newKey: $newKey ?? null ); - } catch (TruncateException) { - throw new Exception($this->getInvalidResizeException()); - } catch (NotFoundException) { - throw new Exception($this->getNotFoundException()); - } catch (LimitException) { - throw new Exception($this->getLimitException()); + } catch (DuplicateException) { + throw new Exception($this->getDuplicateException()); } catch (IndexException $e) { throw new Exception($this->getInvalidIndexException(), $e->getMessage()); + } catch (LimitException) { + throw new Exception($this->getLimitException()); + } catch (TruncateException) { + throw new Exception($this->getInvalidResizeException()); } } @@ -610,7 +609,11 @@ abstract class Action extends UtopiaAction ->setAttribute('$id', ID::custom($db->getSequence() . '_' . $collection->getSequence() . '_' . $newKey)) ->setAttribute('key', $newKey); - $dbForProject->updateDocument('attributes', $originalUid, $attribute); + try { + $dbForProject->updateDocument('attributes', $originalUid, $attribute); + } catch (DuplicateException) { + throw new Exception($this->getDuplicateException()); + } /** * @var Document $index diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php index 6b80901e05..b5e7bf5415 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php @@ -13,7 +13,9 @@ use Appwrite\Utopia\Response as UtopiaResponse; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate as DuplicateException; +use Utopia\Database\Exception\Index as IndexException; use Utopia\Database\Exception\Limit as LimitException; +use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Validator\Authorization; @@ -86,6 +88,7 @@ class Create extends Action $collectionId = $collectionId === 'unique()' ? ID::unique() : $collectionId; + // Map aggregate permissions into the multiple permissions they represent. $permissions = Permission::aggregate($permissions) ?? []; try { @@ -105,6 +108,22 @@ class Create extends Action throw new Exception($this->getDuplicateException()); } catch (LimitException) { throw new Exception($this->getLimitException()); + } catch (NotFoundException) { + throw new Exception(Exception::DATABASE_NOT_FOUND); + } + + try { + $dbForProject->createCollection( + id: 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + permissions: $permissions, + documentSecurity: $documentSecurity + ); + } catch (DuplicateException) { + throw new Exception($this->getDuplicateException()); + } catch (IndexException) { + throw new Exception($this->getInvalidIndexException()); + } catch (LimitException) { + throw new Exception($this->getLimitException()); } $queueForEvents diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 23883c9ff4..aef2ef4ace 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -16,6 +16,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\NotFound as NotFoundException; +use Utopia\Database\Exception\Relationship as RelationshipException; use Utopia\Database\Exception\Structure as StructureException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; @@ -245,12 +246,14 @@ class Create extends Action try { $document = $dbForProject->createDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $document); - } catch (StructureException $e) { - throw new Exception($this->getInvalidStructureException(), $e->getMessage()); } catch (DuplicateException) { throw new Exception($this->getDuplicateException()); } catch (NotFoundException) { throw new Exception($this->getParentNotFoundException()); + } catch (RelationshipException $e) { + throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage()); + } catch (StructureException $e) { + throw new Exception($this->getInvalidStructureException(), $e->getMessage()); } // Add $collectionId and $databaseId for all documents