From e07a1817ab57d9175f9be29d0785fd3429b29ff5 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 11 Jun 2025 13:42:55 +0530 Subject: [PATCH] update: remove conflict, restrict and relationship exceptions from global handler. --- app/controllers/general.php | 19 ---------------- .../Collections/Attributes/Action.php | 22 +++++++++++++++++-- .../Collections/Documents/Action.php | 19 ++++++++++++++++ .../Collections/Documents/Delete.php | 9 +++++--- .../Collections/Documents/Update.php | 12 +++++----- 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 46bc367d63..5aed7c21ea 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -1174,14 +1174,6 @@ App::error() break; } break; - case 'Utopia\Database\Exception\Conflict': - $error = new AppwriteException( - $isTablesAPI - ? AppwriteException::ROW_UPDATE_CONFLICT - : AppwriteException::DOCUMENT_UPDATE_CONFLICT, - previous: $error - ); - break; case 'Utopia\Database\Exception\Timeout': $error = new AppwriteException(AppwriteException::DATABASE_TIMEOUT, previous: $error); break; @@ -1203,20 +1195,9 @@ App::error() ? AppwriteException::ROW_ALREADY_EXISTS : AppwriteException::DOCUMENT_ALREADY_EXISTS ); - break; - case 'Utopia\Database\Exception\Restricted': - $error = new AppwriteException( - $isTablesAPI - ? AppwriteException::ROW_DELETE_RESTRICTED - : AppwriteException::DOCUMENT_DELETE_RESTRICTED - ); - break; case 'Utopia\Database\Exception\Authorization': $error = new AppwriteException(AppwriteException::USER_UNAUTHORIZED); break; - case 'Utopia\Database\Exception\Relationship': - $error = new AppwriteException(AppwriteException::RELATIONSHIP_VALUE_INVALID, $error->getMessage(), previous: $error); - break; case 'Utopia\Database\Exception\NotFound': $error = new AppwriteException( $isTablesAPI 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 2e0e598695..92751bcbe4 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 @@ -14,6 +14,8 @@ 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; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\Authorization; @@ -124,6 +126,16 @@ abstract class Action extends UtopiaAction : Exception::COLUMN_ALREADY_EXISTS; } + /** + * Get the correct invalid structure message. + */ + final protected function getInvalidStructureException(): string + { + return $this->isCollectionsAPI() + ? Exception::DOCUMENT_INVALID_STRUCTURE + : Exception::ROW_INVALID_STRUCTURE; + } + /** * Get the appropriate limit exceeded exception. */ @@ -541,8 +553,14 @@ abstract class Action extends UtopiaAction newKey: $newKey, onDelete: $primaryDocumentOptions['onDelete'], ); - } catch (NotFoundException) { - throw new Exception($this->getNotFoundException()); + } catch (IndexException) { + throw new Exception(Exception::INDEX_INVALID); + } catch (LimitException) { + throw new Exception($this->getLimitException()); + } catch (RelationshipException $e) { + throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage()); + } catch (StructureException $e) { + throw new Exception($this->getInvalidStructureException(), $e->getMessage()); } if ($primaryDocumentOptions['twoWay']) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 01ee2d9f84..32bb314cd2 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -97,6 +97,25 @@ abstract class Action extends UtopiaAction : Exception::ROW_ALREADY_EXISTS; } + /** + * Get the appropriate conflict exception. + */ + final protected function getConflictException(): string + { + return $this->isCollectionsAPI() + ? Exception::DOCUMENT_UPDATE_CONFLICT + : Exception::ROW_UPDATE_CONFLICT; + } + + /** + * Get the appropriate delete restricted exception. + */ + final protected function getRestrictedException(): string + { + return $this->isCollectionsAPI() + ? Exception::DOCUMENT_DELETE_RESTRICTED + : Exception::ROW_DELETE_RESTRICTED; + } /** * Get the correct invalid structure message. diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php index 32af5b0127..9944de4e36 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php @@ -13,7 +13,8 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response as UtopiaResponse; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Database\Exception\NotFound as NotFoundException; +use Utopia\Database\Exception\Conflict as ConflictException; +use Utopia\Database\Exception\Restricted as RestrictedException; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Scope\HTTP; @@ -108,8 +109,10 @@ class Delete extends Action $documentId ); }); - } catch (NotFoundException) { - throw new Exception($this->getParentNotFoundException()); + } catch (ConflictException) { + throw new Exception($this->getConflictException()); + } catch (RestrictedException) { + throw new Exception($this->getRestrictedException()); } // Add $collection and $databaseId for all documents diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php index 58408ae4ab..c3a2fe6df3 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php @@ -13,9 +13,9 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response as UtopiaResponse; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Database\Exception\Authorization as AuthorizationException; +use Utopia\Database\Exception\Conflict as ConflictException; 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; @@ -239,14 +239,14 @@ class Update extends Action $newDocument ) ); - } catch (AuthorizationException) { - throw new Exception(Exception::USER_UNAUTHORIZED); + } catch (ConflictException) { + throw new Exception($this->getConflictException()); } catch (DuplicateException) { throw new Exception($this->getDuplicateException()); + } catch (RelationshipException $e) { + throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage()); } catch (StructureException $e) { throw new Exception($this->getInvalidStructureException(), $e->getMessage()); - } catch (NotFoundException) { - throw new Exception($this->getParentNotFoundException()); } // Add $collectionId and $databaseId for all documents