From 5fee7378ad1883fa0b9693313b07f63574cba512 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 13 Feb 2022 13:12:16 +0400 Subject: [PATCH] feat: add test for error message --- app/config/errors.php | 5 +++++ app/controllers/general.php | 19 +++++++++++++++---- src/Appwrite/Extend/Exception.php | 1 + tests/e2e/General/HTTPTest.php | 3 ++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 6398ac808..e0ebf3c6c 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -58,6 +58,11 @@ return [ 'description' => 'The query\'s syntax is invalid. Please check the query and try again.', 'code' => 400, ], + Exception::GENERAL_ROUTE_NOT_FOUND => [ + 'name' => Exception::GENERAL_ROUTE_NOT_FOUND, + 'description' => 'The requested route was not found. Please refer to the docs and try again.', + 'code' => 404, + ], Exception::GENERAL_CURSOR_NOT_FOUND => [ 'name' => Exception::GENERAL_CURSOR_NOT_FOUND, 'description' => 'The cursor is invalid. This can happen if the item represented by the cursor has been deleted.', diff --git a/app/controllers/general.php b/app/controllers/general.php index 55517fff9..37be00f0f 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -375,14 +375,25 @@ App::error(function ($error, $utopia, $request, $response, $layout, $project, $l throw $error; } + + /** Handle Utopia Errors */ + if ($error instanceof Utopia\Exception) { + $code = $error->getCode(); + $error = new Exception($error->getMessage(), $code, Exception::GENERAL_UNKNOWN, $error); + switch($code) { + case 400: + $error->setType(Exception::GENERAL_ARGUMENT_INVALID); + break; + case 404: + $error->setType(Exception::GENERAL_ROUTE_NOT_FOUND); + break; + } + } + /** Wrap all exceptions inside Appwrite\Extend\Exception */ if (!($error instanceof Exception)) { $error = new Exception($error->getMessage(), $error->getCode(), Exception::GENERAL_UNKNOWN, $error); } - - if ($error instanceof Utopia\Exception && $error->getCode() === 400) { - $error->setType(Exception::GENERAL_ARGUMENT_INVALID); - } $template = ($route) ? $route->getLabel('error', null) : null; diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 5b05551b4..71b2ff95d 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -42,6 +42,7 @@ class Exception extends \Exception const GENERAL_ARGUMENT_INVALID = 'general_argument_invalid'; const GENERAL_QUERY_LIMIT_EXCEEDED = 'general_query_limit_exceeded'; const GENERAL_QUERY_INVALID = 'general_query_invalid'; + const GENERAL_ROUTE_NOT_FOUND = 'general_route_not_found'; const GENERAL_CURSOR_NOT_FOUND = 'general_cursor_not_found'; const GENERAL_SERVER_ERROR = 'general_server_error'; diff --git a/tests/e2e/General/HTTPTest.php b/tests/e2e/General/HTTPTest.php index 4b26f2fcd..696c3a320 100644 --- a/tests/e2e/General/HTTPTest.php +++ b/tests/e2e/General/HTTPTest.php @@ -2,7 +2,7 @@ namespace Tests\E2E\General; -use Exception; +use Appwrite\Extend\Exception; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectNone; use Tests\E2E\Scopes\Scope; @@ -45,6 +45,7 @@ class HTTPTest extends Scope $this->assertEquals(404, $response['headers']['status-code']); $this->assertEquals('Not Found', $response['body']['message']); + $this->assertEquals(Exception::GENERAL_ROUTE_NOT_FOUND, $response['body']['type']); $this->assertEquals(404, $response['body']['code']); $this->assertEquals('dev', $response['body']['version']); }