From 1a7e65d3392e9452e552bfe881c2aa3e1b786cf0 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 19 Jan 2023 11:19:16 +0200 Subject: [PATCH] error resource changes --- app/config/errors.php | 20 +++++++------- app/controllers/api/databases.php | 27 ++++++++++--------- app/controllers/general.php | 6 +++++ composer.json | 2 +- composer.lock | 23 ++++++++++------ docker-compose.yml | 1 + src/Appwrite/Extend/Exception.php | 3 +++ .../e2e/Services/Databases/DatabasesBase.php | 22 ++++++--------- 8 files changed, 58 insertions(+), 46 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 8eae95919c..c4a9869c7e 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -559,14 +559,14 @@ return [ 'description' => 'Too many queries.', 'code' => 400, ], -// Exception::TIMEOUT_BLOCKED => [ -// 'name' => Exception::TIMEOUT_BLOCKED, -// 'description' => Exception::TIMEOUT_BLOCKED, -// 'code' => 403, -// ], -// Exception::TIMEOUT => [ -// 'name' => Exception::TIMEOUT, -// 'description' => Exception::TIMEOUT, -// 'code' => 408, -// ], + Exception::TIMEOUT_BLOCKED => [ + 'name' => Exception::TIMEOUT_BLOCKED, + 'description' => Exception::TIMEOUT_BLOCKED, + 'code' => 403, + ], + Exception::TIMEOUT => [ + 'name' => Exception::TIMEOUT, + 'description' => Exception::TIMEOUT, + 'code' => 408, + ], ]; diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index dcbf047ad5..e639dd6cf0 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -151,7 +151,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att App::init() - //->groups(['timeout']) + ->groups(['timeout']) ->inject('request') ->inject('dbForProject') ->action(function (Request $request, Database $dbForProject) { @@ -174,16 +174,19 @@ App::init() }); App::error() - //->groups(['timeout']) + ->groups(['timeout']) ->inject('utopia') ->inject('error') ->inject('request') ->inject('dbForProject') ->action(function (App $utopia, throwable $error, Request $request, Database $dbForProject) { + + var_dump("App::error start instanceof Timeout -------------------------------------"); + var_dump($error->getMessage()); + var_dump($request->getParams()); + if ($error instanceof Timeout) { - var_dump("App::error start instanceof Timeout -------------------------------------"); - var_dump($error->getMessage()); - var_dump($request->getParams()); + $error = new Exception(Exception::TIMEOUT); $queries = $request->getParam('queries'); // validate malicious $uri = $request->getURI(); $key = md5(json_encode([$uri, $queries])); @@ -204,7 +207,7 @@ App::error() } else { var_dump("updateDocument start! updateDocument updateDocument updateDocument updateDocument updateDocument updateDocument"); $document['count']++; - if ($document['count'] > 1) { // todo: make this configurable + if ($document['count'] > 1) { // todo: make this configurable exceeds max $document['blocked'] = true; } $document = Authorization::skip(fn() => $dbForProject->updateDocument('timeouts', $document->getId(), $document)); @@ -213,15 +216,13 @@ App::error() if ($document['blocked'] === true) { var_dump("blocked throwing exception"); - throw new Exception(Exception::TIMEOUT_BLOCKED); + $error = new Exception(Exception::TIMEOUT_BLOCKED); } - - var_dump("App::error end !! instanceof Timeout-------------------------------------"); - - //throw new Exception(Exception::TIMEOUT); } - throw $error; + App::setResource('error', fn() => $error); + + var_dump("App::error end !! instanceof Timeout-------------------------------------"); }); @@ -2041,7 +2042,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') App::get('/v1/databases/:databaseId/collections/:collectionId/documents') ->alias('/v1/database/collections/:collectionId/documents', ['databaseId' => 'default']) ->desc('List Documents') - ->groups(['api', 'database']) + ->groups(['api', 'database','timeout']) ->label('scope', 'documents.read') ->label('usage.metric', 'documents.{scope}.requests.read') ->label('usage.params', ['databaseId:{request.databaseId}', 'collectionId:{request.collectionId}']) diff --git a/app/controllers/general.php b/app/controllers/general.php index d3009991d3..ffd81072ca 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -401,6 +401,11 @@ App::error() ->inject('loggerBreadcrumbs') ->action(function (Throwable $error, App $utopia, Request $request, Response $response, Document $project, ?Logger $logger, array $loggerBreadcrumbs) { + + var_dump($error->getCode()); + var_dump($error->getMessage()); + var_dump(get_class($error)); + $version = App::getEnv('_APP_VERSION', 'UNKNOWN'); $route = $utopia->match($request); @@ -498,6 +503,7 @@ App::error() case 402: // Error allowed publicly case 403: // Error allowed publicly case 404: // Error allowed publicly + case 408: // Error allowed publicly case 409: // Error allowed publicly case 412: // Error allowed publicly case 416: // Error allowed publicly diff --git a/composer.json b/composer.json index 71379adc5f..f1e6c7804c 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,7 @@ "utopia-php/database": "dev-timeout as 0.28.0", "utopia-php/mongo": "dev-main as 0.0.2", "utopia-php/domains": "1.1.*", - "utopia-php/framework": "0.25.*", + "utopia-php/framework": "dev-feat-allow-error-overrides as 0.25.1", "utopia-php/image": "0.5.*", "utopia-php/locale": "0.4.*", "utopia-php/logger": "0.3.*", diff --git a/composer.lock b/composer.lock index 1971c1f703..c12c2a836c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1eda50d79bec550d145441de27ad6ec9", + "content-hash": "a7c39b09f9e7d32796ec397958fa4b9b", "packages": [ { "name": "adhocore/jwt", @@ -2226,16 +2226,16 @@ }, { "name": "utopia-php/framework", - "version": "0.25.1", + "version": "dev-feat-allow-error-overrides", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "2391b397135586b2100d39e338827bef8d2f4ad0" + "reference": "784cb4ffbf621663321aafe1a893e344756c491f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/2391b397135586b2100d39e338827bef8d2f4ad0", - "reference": "2391b397135586b2100d39e338827bef8d2f4ad0", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/784cb4ffbf621663321aafe1a893e344756c491f", + "reference": "784cb4ffbf621663321aafe1a893e344756c491f", "shasum": "" }, "require": { @@ -2264,9 +2264,9 @@ ], "support": { "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.25.1" + "source": "https://github.com/utopia-php/framework/tree/feat-allow-error-overrides" }, - "time": "2022-11-23T18:22:23+00:00" + "time": "2023-01-19T08:53:42+00:00" }, { "name": "utopia-php/image", @@ -5593,6 +5593,12 @@ "alias": "0.28.0", "alias_normalized": "0.28.0.0" }, + { + "package": "utopia-php/framework", + "version": "dev-feat-allow-error-overrides", + "alias": "0.25.1", + "alias_normalized": "0.25.1.0" + }, { "package": "utopia-php/mongo", "version": "dev-main", @@ -5603,7 +5609,8 @@ "minimum-stability": "stable", "stability-flags": { "utopia-php/database": 20, - "utopia-php/mongo": 20 + "utopia-php/mongo": 20, + "utopia-php/framework": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/docker-compose.yml b/docker-compose.yml index df1d5a78f3..a116c460b3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -80,6 +80,7 @@ services: - ./phpunit.xml:/usr/src/code/phpunit.xml - ./tests:/usr/src/code/tests - ./app:/usr/src/code/app + #- ./vendor/utopia-php/framework:/usr/src/code/vendor/utopia-php/framework - ./docs:/usr/src/code/docs - ./public:/usr/src/code/public - ./src:/usr/src/code/src diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index e8ac9cfe57..2c656ecdbc 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -200,6 +200,9 @@ class Exception extends \Exception $this->message = $message ?? $this->message; $this->code = $code ?? $this->code; + var_dump("__construct __construct __construct __construct __construct __construct __construct"); + var_dump($this->code); + var_dump("__construct __construct __construct __construct __construct __construct __construct"); parent::__construct($this->message, $this->code, $previous); } diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index a49702bb8a..98a6270ad0 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -990,17 +990,14 @@ trait DatabasesBase */ public function testCreateTimeout(array $data): void { - //sleep(1); - $databaseId = $data['databaseId']; - - $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents', array_merge([ + $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['moviesId'] . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => ['sleep("$id", 1)'], ]); - $this->assertEquals(500, $documents['headers']['status-code']); + $this->assertEquals(408, $documents['headers']['status-code']); } @@ -1009,35 +1006,32 @@ trait DatabasesBase */ public function testUpdateAndBlockTimeout(array $data): void { - // sleep(1); - $databaseId = $data['databaseId']; - $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents', array_merge([ + $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['moviesId'] . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => ['sleep("$id", 1)'], ]); - $this->assertEquals(500, $documents['headers']['status-code']); + $this->assertEquals(403, $documents['headers']['status-code']); } /** * @depends testCreateDocument */ - public function testBlockedAtInitTimeout(array $data): void + public function testBlockedAtInitTimeout(array $data): array { - // sleep(1); - $databaseId = $data['databaseId']; - $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents', array_merge([ + $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['moviesId'] . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => ['sleep("$id", 1)'], ]); - $this->assertEquals(500, $documents['headers']['status-code']); + $this->assertEquals(403, $documents['headers']['status-code']); exit; + } /**