mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
set setResource return
This commit is contained in:
@@ -172,56 +172,55 @@ App::error()
|
||||
->inject('request')
|
||||
->inject('dbForProject')
|
||||
->action(function (Throwable $error, Request $request, Database $dbForProject) {
|
||||
try {
|
||||
if ($error instanceof Timeout) {
|
||||
$route = Request::getRoute();
|
||||
$collectionId = $route->getParamValue('collectionId');
|
||||
$databaseId = $route->getParamValue('databaseId');
|
||||
$queries = $request->getParam('queries', []);
|
||||
if ($error instanceof Timeout) {
|
||||
$route = Request::getRoute();
|
||||
$collectionId = $route->getParamValue('collectionId');
|
||||
$databaseId = $route->getParamValue('databaseId');
|
||||
$queries = $request->getParam('queries', []);
|
||||
|
||||
$queriesValidator = new ArrayList(new Text(APP_LIMIT_ARRAY_ELEMENT_SIZE), APP_LIMIT_ARRAY_PARAMS_SIZE);
|
||||
if (!$queriesValidator->isValid($queries)) {
|
||||
throw new Exception(Exception::GENERAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
$key = md5(json_encode([
|
||||
$request->getMethod(),
|
||||
$request->getURI(), // Contains databaseId & collectionId
|
||||
$request->getParam('queries')
|
||||
]));
|
||||
|
||||
/** @var Document $document */
|
||||
$document = Authorization::skip(fn() => $dbForProject->getDocument('slowQueries', $key));
|
||||
if ($document->isEmpty()) {
|
||||
$document = Authorization::skip(fn()=>$dbForProject->createDocument('slowQueries', new Document([
|
||||
'$id' => $key,
|
||||
'blocked' => false,
|
||||
'count' => 1,
|
||||
'queries' => $queries,
|
||||
'databaseId' => $databaseId,
|
||||
'collectionId' => $collectionId,
|
||||
'path' => $request->getURI()
|
||||
])));
|
||||
} else {
|
||||
$document->setAttribute('count', $document->getAttribute('count') + 1);
|
||||
$max = intval(App::getEnv('_APP_SLOW_QUERIES_MAX_HITS', 9999));
|
||||
if ($document->getAttribute('count') >= $max) {
|
||||
$document->setAttribute('blocked', true);
|
||||
}
|
||||
$document = Authorization::skip(fn() => $dbForProject->updateDocument('slowQueries', $document->getId(), $document));
|
||||
}
|
||||
|
||||
if ($document->getAttribute('blocked') === true) {
|
||||
throw new Exception(Exception::QUERY_BLOCKED);
|
||||
}
|
||||
|
||||
throw new Exception(Exception::QUERY_TIMEOUT);
|
||||
$queriesValidator = new ArrayList(new Text(APP_LIMIT_ARRAY_ELEMENT_SIZE), APP_LIMIT_ARRAY_PARAMS_SIZE);
|
||||
if (!$queriesValidator->isValid($queries)) {
|
||||
App::setResource('error', fn() => new Exception(Exception::GENERAL_SERVER_ERROR));
|
||||
return;
|
||||
}
|
||||
|
||||
throw $error;
|
||||
} catch (throwable $error) {
|
||||
App::setResource('error', fn() => $error);
|
||||
$key = md5(json_encode([
|
||||
$request->getMethod(),
|
||||
$request->getURI(), // Contains databaseId & collectionId
|
||||
$request->getParam('queries')
|
||||
]));
|
||||
|
||||
/** @var Document $document */
|
||||
$document = Authorization::skip(fn() => $dbForProject->getDocument('slowQueries', $key));
|
||||
if ($document->isEmpty()) {
|
||||
$document = Authorization::skip(fn()=>$dbForProject->createDocument('slowQueries', new Document([
|
||||
'$id' => $key,
|
||||
'blocked' => false,
|
||||
'count' => 1,
|
||||
'queries' => $queries,
|
||||
'databaseId' => $databaseId,
|
||||
'collectionId' => $collectionId,
|
||||
'path' => $request->getURI()
|
||||
])));
|
||||
} else {
|
||||
$document->setAttribute('count', $document->getAttribute('count') + 1);
|
||||
$max = intval(App::getEnv('_APP_SLOW_QUERIES_MAX_HITS', 9999));
|
||||
if ($document->getAttribute('count') >= $max) {
|
||||
$document->setAttribute('blocked', true);
|
||||
}
|
||||
$document = Authorization::skip(fn() => $dbForProject->updateDocument('slowQueries', $document->getId(), $document));
|
||||
}
|
||||
|
||||
if ($document->getAttribute('blocked') === true) {
|
||||
App::setResource('error', fn() => new Exception(Exception::QUERY_BLOCKED));
|
||||
return;
|
||||
}
|
||||
|
||||
App::setResource('error', fn() => new Exception(Exception::QUERY_TIMEOUT));
|
||||
return;
|
||||
}
|
||||
|
||||
App::setResource('error', fn() => $error);
|
||||
});
|
||||
|
||||
App::post('/v1/databases')
|
||||
|
||||
Reference in New Issue
Block a user