set setResource return

This commit is contained in:
fogelito
2023-03-06 10:41:50 +02:00
parent 13b52a67a9
commit 2a7ca5fe67
3 changed files with 67 additions and 69 deletions
+45 -46
View File
@@ -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')