From 3a88c74109a1a1fea51dd8e3ea4b902d9259264e Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 11 Jun 2025 16:18:06 +0530 Subject: [PATCH] update: remove structure exceptions from global handler. --- app/controllers/general.php | 9 --- .../Collections/Attributes/Action.php | 11 ++-- .../Collections/Documents/Update.php | 2 - .../Databases/Http/Databases/Create.php | 60 ++++++++++--------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 9d9a03db92..0d0a253661 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -1177,15 +1177,6 @@ App::error() case 'Utopia\Database\Exception\Timeout': $error = new AppwriteException(AppwriteException::DATABASE_TIMEOUT, previous: $error); break; - case 'Utopia\Database\Exception\Structure': - $error = new AppwriteException( - $isTablesAPI - ? AppwriteException::ROW_INVALID_STRUCTURE - : AppwriteException::DOCUMENT_INVALID_STRUCTURE, - $error->getMessage(), - previous: $error - ); - break; case 'Utopia\Database\Exception\Duplicate': $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 92751bcbe4..3939a0370c 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 @@ -395,17 +395,20 @@ abstract class Action extends UtopiaAction $dbForProject->checkAttribute($relatedCollection, $twoWayAttribute); $dbForProject->createDocument('attributes', $twoWayAttribute); } catch (DuplicateException) { - $dbForProject->deleteDocument('attributes', $attribute->getId()); throw new Exception($this->getDuplicateException()); } catch (LimitException) { - $dbForProject->deleteDocument('attributes', $attribute->getId()); throw new Exception($this->getLimitException()); + } catch (StructureException) { + throw new Exception($this->getInvalidStructureException()); } catch (Throwable $e) { - $dbForProject->purgeCachedDocument('database_' . $db->getSequence(), $relatedCollection->getId()); - $dbForProject->purgeCachedCollection('database_' . $db->getSequence() . '_collection_' . $relatedCollection->getSequence()); + $dbForProject->deleteDocument('attributes', $attribute->getId()); throw $e; + } finally { + $dbForProject->purgeCachedDocument('database_' . $db->getSequence(), $collectionId); + $dbForProject->purgeCachedCollection('database_' . $db->getSequence() . '_collection_' . $collection->getSequence()); } + // If operation succeeded, purge the cache for the related collection too $dbForProject->purgeCachedDocument('database_' . $db->getSequence(), $relatedCollection->getId()); $dbForProject->purgeCachedCollection('database_' . $db->getSequence() . '_collection_' . $relatedCollection->getSequence()); } 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 c3a2fe6df3..f69dfb3f98 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 @@ -228,8 +228,6 @@ class Update extends Action ->addMetric(METRIC_DATABASES_OPERATIONS_WRITES, max($operations, 1)) ->addMetric(str_replace('{databaseInternalId}', $database->getSequence(), METRIC_DATABASE_ID_OPERATIONS_WRITES), $operations); - $response->addHeader('X-Debug-Operations', $operations); - try { $document = $dbForProject->withRequestTimestamp( $requestTimestamp, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php index 1f368ec77a..6e4dc5e136 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Create.php @@ -14,6 +14,9 @@ use Utopia\Config\Config; 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\Structure as StructureException; use Utopia\Database\Helpers\ID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -76,42 +79,41 @@ class Create extends Action 'enabled' => $enabled, 'search' => implode(' ', [$databaseId, $name]), ])); - $database = $dbForProject->getDocument('databases', $databaseId); + } catch (DuplicateException) { + throw new Exception(Exception::DATABASE_ALREADY_EXISTS); + } catch (StructureException $e) { + // TODO: @Jake, how do we handle this document/row? + // there's no context awareness at this level on what the api is. + throw new Exception(Exception::DOCUMENT_INVALID_STRUCTURE, $e->getMessage()); + } - $collections = (Config::getParam('collections', [])['databases'] ?? [])['collections'] ?? []; - if (empty($collections)) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'The "collections" collection is not configured.'); - } + $database = $dbForProject->getDocument('databases', $databaseId); - $attributes = []; - $indexes = []; + $collections = (Config::getParam('collections', [])['databases'] ?? [])['collections'] ?? []; + if (empty($collections)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'The "collections" collection is not configured.'); + } - foreach ($collections['attributes'] as $attribute) { - $attributes[] = new Document([ - '$id' => $attribute['$id'], - 'type' => $attribute['type'], - 'size' => $attribute['size'], - 'required' => $attribute['required'], - 'signed' => $attribute['signed'], - 'array' => $attribute['array'], - 'filters' => $attribute['filters'], - 'default' => $attribute['default'] ?? null, - 'format' => $attribute['format'] ?? '' - ]); - } + $attributes = []; + foreach ($collections['attributes'] as $attribute) { + $attributes[] = new Document($attribute); + } - foreach ($collections['indexes'] as $index) { - $indexes[] = new Document([ - '$id' => $index['$id'], - 'type' => $index['type'], - 'attributes' => $index['attributes'], - 'lengths' => $index['lengths'], - 'orders' => $index['orders'], - ]); - } + $indexes = []; + foreach ($collections['indexes'] as $index) { + $indexes[] = new Document($index); + } + + try { $dbForProject->createCollection('database_' . $database->getSequence(), $attributes, $indexes); } catch (DuplicateException) { throw new Exception(Exception::DATABASE_ALREADY_EXISTS); + } catch (IndexException) { + throw new Exception(Exception::INDEX_INVALID); + } catch (LimitException) { + // TODO: @Jake, how do we handle this collection/table? + // there's no context awareness at this level on what the api is. + throw new Exception(Exception::COLLECTION_LIMIT_EXCEEDED); } $queueForEvents->setParam('databaseId', $database->getId());