mirror of
https://github.com/appwrite/appwrite
synced 2026-05-24 09:28:40 +00:00
update: remove query exceptions from global handler.
This commit is contained in:
parent
e07a1817ab
commit
e6268fe5a6
8 changed files with 56 additions and 27 deletions
|
|
@ -1177,9 +1177,6 @@ App::error()
|
||||||
case 'Utopia\Database\Exception\Timeout':
|
case 'Utopia\Database\Exception\Timeout':
|
||||||
$error = new AppwriteException(AppwriteException::DATABASE_TIMEOUT, previous: $error);
|
$error = new AppwriteException(AppwriteException::DATABASE_TIMEOUT, previous: $error);
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Database\Exception\Query':
|
|
||||||
$error = new AppwriteException(AppwriteException::GENERAL_QUERY_INVALID, $error->getMessage(), previous: $error);
|
|
||||||
break;
|
|
||||||
case 'Utopia\Database\Exception\Structure':
|
case 'Utopia\Database\Exception\Structure':
|
||||||
$error = new AppwriteException(
|
$error = new AppwriteException(
|
||||||
$isTablesAPI
|
$isTablesAPI
|
||||||
|
|
@ -1195,6 +1192,7 @@ App::error()
|
||||||
? AppwriteException::ROW_ALREADY_EXISTS
|
? AppwriteException::ROW_ALREADY_EXISTS
|
||||||
: AppwriteException::DOCUMENT_ALREADY_EXISTS
|
: AppwriteException::DOCUMENT_ALREADY_EXISTS
|
||||||
);
|
);
|
||||||
|
// no break
|
||||||
case 'Utopia\Database\Exception\Authorization':
|
case 'Utopia\Database\Exception\Authorization':
|
||||||
$error = new AppwriteException(AppwriteException::USER_UNAUTHORIZED);
|
$error = new AppwriteException(AppwriteException::USER_UNAUTHORIZED);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use Appwrite\Utopia\Response as UtopiaResponse;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Database\Exception\Order as OrderException;
|
use Utopia\Database\Exception\Order as OrderException;
|
||||||
|
use Utopia\Database\Exception\Query as QueryException;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
use Utopia\Database\Validator\Authorization;
|
use Utopia\Database\Validator\Authorization;
|
||||||
use Utopia\Database\Validator\Query\Cursor;
|
use Utopia\Database\Validator\Query\Cursor;
|
||||||
|
|
@ -74,7 +75,11 @@ class XList extends Action
|
||||||
throw new Exception($this->getParentNotFoundException());
|
throw new Exception($this->getParentNotFoundException());
|
||||||
}
|
}
|
||||||
|
|
||||||
$queries = Query::parseQueries($queries);
|
try {
|
||||||
|
$queries = Query::parseQueries($queries);
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
\array_push(
|
\array_push(
|
||||||
$queries,
|
$queries,
|
||||||
|
|
@ -95,33 +100,43 @@ class XList extends Action
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributeId = $cursor->getValue();
|
$attributeId = $cursor->getValue();
|
||||||
$cursorDocument = Authorization::skip(
|
try {
|
||||||
fn () => $dbForProject->find('attributes', [
|
$cursorDocument = $dbForProject->findOne('attributes', [
|
||||||
Query::equal('databaseInternalId', [$database->getSequence()]),
|
Query::equal('databaseInternalId', [$database->getSequence()]),
|
||||||
Query::equal('collectionInternalId', [$collection->getSequence()]),
|
Query::equal('collectionInternalId', [$collection->getSequence()]),
|
||||||
Query::equal('key', [$attributeId]),
|
Query::equal('key', [$attributeId]),
|
||||||
Query::limit(1),
|
]);
|
||||||
])
|
} catch (QueryException $e) {
|
||||||
);
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($cursorDocument) || $cursorDocument[0]->isEmpty()) {
|
if ($cursorDocument->isEmpty()) {
|
||||||
$type = ucfirst($this->getContext());
|
$type = ucfirst($this->getContext());
|
||||||
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "$type '$attributeId' for the 'cursor' value not found.");
|
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "$type '$attributeId' for the 'cursor' value not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$cursor->setValue($cursorDocument[0]);
|
$cursor->setValue($cursorDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
$filters = Query::groupByType($queries)['filters'];
|
$filterQueries = Query::groupByType($queries)['filters'];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$attributes = $dbForProject->find('attributes', $queries);
|
$attributes = $dbForProject->find('attributes', $queries);
|
||||||
$total = $dbForProject->count('attributes', $filters, APP_LIMIT_COUNT);
|
$total = $dbForProject->count('attributes', $filterQueries, APP_LIMIT_COUNT);
|
||||||
} catch (OrderException $e) {
|
} catch (OrderException $e) {
|
||||||
$documents = $this->isCollectionsAPI() ? 'documents' : 'rows';
|
$documents = $this->isCollectionsAPI() ? 'documents' : 'rows';
|
||||||
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
||||||
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
||||||
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
||||||
|
} catch (QueryException) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($attributes as $attribute) {
|
||||||
|
if ($attribute->getAttribute('type') === Database::VAR_STRING) {
|
||||||
|
$filters = $attribute->getAttribute('filters', []);
|
||||||
|
$attribute->setAttribute('encrypt', in_array('encrypt', $filters));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ use Appwrite\SDK\Response as SDKResponse;
|
||||||
use Appwrite\Utopia\Response as UtopiaResponse;
|
use Appwrite\Utopia\Response as UtopiaResponse;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Database\Exception\Authorization as AuthorizationException;
|
|
||||||
use Utopia\Database\Exception\Query as QueryException;
|
use Utopia\Database\Exception\Query as QueryException;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
use Utopia\Database\Validator\Authorization;
|
use Utopia\Database\Validator\Authorization;
|
||||||
|
|
@ -71,10 +70,10 @@ class Get extends Action
|
||||||
|
|
||||||
public function action(string $databaseId, string $collectionId, string $documentId, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage): void
|
public function action(string $databaseId, string $collectionId, string $documentId, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage): void
|
||||||
{
|
{
|
||||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
|
||||||
$isAPIKey = Auth::isAppUser(Authorization::getRoles());
|
$isAPIKey = Auth::isAppUser(Authorization::getRoles());
|
||||||
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
|
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
|
||||||
|
|
||||||
|
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||||
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
|
if ($database->isEmpty() || (!$database->getAttribute('enabled', false) && !$isAPIKey && !$isPrivilegedUser)) {
|
||||||
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
@ -87,9 +86,12 @@ class Get extends Action
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$queries = Query::parseQueries($queries);
|
$queries = Query::parseQueries($queries);
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
$document = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId, $queries);
|
$document = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId, $queries);
|
||||||
} catch (AuthorizationException) {
|
|
||||||
throw new Exception(Exception::USER_UNAUTHORIZED);
|
|
||||||
} catch (QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,19 +77,16 @@ class XList extends Action
|
||||||
public function action(string $databaseId, string $collectionId, string $documentId, array $queries, UtopiaResponse $response, Database $dbForProject, Locale $locale, Reader $geodb): void
|
public function action(string $databaseId, string $collectionId, string $documentId, array $queries, UtopiaResponse $response, Database $dbForProject, Locale $locale, Reader $geodb): void
|
||||||
{
|
{
|
||||||
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
|
||||||
|
|
||||||
if ($database->isEmpty()) {
|
if ($database->isEmpty()) {
|
||||||
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
|
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
|
||||||
|
|
||||||
if ($collection->isEmpty()) {
|
if ($collection->isEmpty()) {
|
||||||
throw new Exception($this->getParentNotFoundException());
|
throw new Exception($this->getParentNotFoundException());
|
||||||
}
|
}
|
||||||
|
|
||||||
$document = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId);
|
$document = $dbForProject->getDocument('database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), $documentId);
|
||||||
|
|
||||||
if ($document->isEmpty()) {
|
if ($document->isEmpty()) {
|
||||||
throw new Exception($this->getNotFoundException());
|
throw new Exception($this->getNotFoundException());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,8 @@ class XList extends Action
|
||||||
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
||||||
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
||||||
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
$operations = 0;
|
$operations = 0;
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use Appwrite\Utopia\Response as UtopiaResponse;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Database\Exception\Order as OrderException;
|
use Utopia\Database\Exception\Order as OrderException;
|
||||||
|
use Utopia\Database\Exception\Query as QueryException;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
use Utopia\Database\Validator\Authorization;
|
use Utopia\Database\Validator\Authorization;
|
||||||
use Utopia\Database\Validator\Query\Cursor;
|
use Utopia\Database\Validator\Query\Cursor;
|
||||||
|
|
@ -80,7 +81,11 @@ class XList extends Action
|
||||||
throw new Exception($this->getGrandParentNotFoundException());
|
throw new Exception($this->getGrandParentNotFoundException());
|
||||||
}
|
}
|
||||||
|
|
||||||
$queries = Query::parseQueries($queries);
|
try {
|
||||||
|
$queries = Query::parseQueries($queries);
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
\array_push(
|
\array_push(
|
||||||
$queries,
|
$queries,
|
||||||
|
|
@ -126,6 +131,8 @@ class XList extends Action
|
||||||
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
||||||
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
||||||
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use Appwrite\Utopia\Response as UtopiaResponse;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Database\Exception\Order as OrderException;
|
use Utopia\Database\Exception\Order as OrderException;
|
||||||
|
use Utopia\Database\Exception\Query as QueryException;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
use Utopia\Database\Validator\Authorization;
|
use Utopia\Database\Validator\Authorization;
|
||||||
use Utopia\Database\Validator\Query\Cursor;
|
use Utopia\Database\Validator\Query\Cursor;
|
||||||
|
|
@ -73,7 +74,11 @@ class XList extends Action
|
||||||
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
throw new Exception(Exception::DATABASE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
$queries = Query::parseQueries($queries);
|
try {
|
||||||
|
$queries = Query::parseQueries($queries);
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($search)) {
|
if (!empty($search)) {
|
||||||
$queries[] = Query::search('search', $search);
|
$queries[] = Query::search('search', $search);
|
||||||
|
|
@ -108,11 +113,10 @@ class XList extends Action
|
||||||
try {
|
try {
|
||||||
$collections = $dbForProject->find('database_' . $database->getSequence(), $queries);
|
$collections = $dbForProject->find('database_' . $database->getSequence(), $queries);
|
||||||
$total = $dbForProject->count('database_' . $database->getSequence(), $filterQueries, APP_LIMIT_COUNT);
|
$total = $dbForProject->count('database_' . $database->getSequence(), $filterQueries, APP_LIMIT_COUNT);
|
||||||
} catch (OrderException $e) {
|
} catch (OrderException) {
|
||||||
$documents = $this->isCollectionsAPI() ? 'documents' : 'rows';
|
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL);
|
||||||
$attribute = $this->isCollectionsAPI() ? 'attribute' : 'column';
|
} catch (QueryException) {
|
||||||
$message = "The order $attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all $documents order $attribute values are non-null.";
|
throw new Exception(Exception::GENERAL_QUERY_INVALID);
|
||||||
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, $message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ use Appwrite\Utopia\Response as UtopiaResponse;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Database\Exception\Order as OrderException;
|
use Utopia\Database\Exception\Order as OrderException;
|
||||||
|
use Utopia\Database\Exception\Query as QueryException;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
use Utopia\Database\Validator\Query\Cursor;
|
use Utopia\Database\Validator\Query\Cursor;
|
||||||
use Utopia\Platform\Action;
|
use Utopia\Platform\Action;
|
||||||
|
|
@ -98,7 +99,10 @@ class XList extends Action
|
||||||
$total = $dbForProject->count('databases', $filterQueries, APP_LIMIT_COUNT);
|
$total = $dbForProject->count('databases', $filterQueries, APP_LIMIT_COUNT);
|
||||||
} catch (OrderException $e) {
|
} catch (OrderException $e) {
|
||||||
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order column '{$e->getAttribute()}' had a null value. Cursor pagination requires all rows order column values are non-null.");
|
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order column '{$e->getAttribute()}' had a null value. Cursor pagination requires all rows order column values are non-null.");
|
||||||
|
} catch (QueryException) {
|
||||||
|
throw new Exception(Exception::GENERAL_QUERY_INVALID);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'databases' => $databases,
|
'databases' => $databases,
|
||||||
'total' => $total,
|
'total' => $total,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue