diff --git a/app/config/errors.php b/app/config/errors.php index e0ebf3c6c..a77388804 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -13,6 +13,11 @@ return [ 'description' => 'An unknown error has occured. Please check the logs for more information.', 'code' => 500, ], + Exception::GENERAL_MOCK => [ + 'name' => Exception::GENERAL_MOCK, + 'description' => 'General errors thrown by the mock controller used for testing.', + 'code' => 400, + ], Exception::GENERAL_ACCESS_FORBIDDEN => [ 'name' => Exception::GENERAL_ACCESS_FORBIDDEN, 'description' => 'Access to this API is forbidden.', diff --git a/app/controllers/mock.php b/app/controllers/mock.php index ea437bf3c..a45bdf641 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -2,6 +2,7 @@ global $utopia, $request, $response; +use Appwrite\Extend\Exception; use Utopia\Database\Document; use Appwrite\Network\Validator\Host; use Appwrite\Utopia\Response; @@ -251,32 +252,32 @@ App::post('/v1/mock/tests/general/upload') $file['size'] = (\is_array($file['size'])) ? $file['size'] : [$file['size']]; if(is_null($start) || is_null($end) || is_null($size)) { - throw new Exception('Invalid content-range header', 400); + throw new Exception('Invalid content-range header', 400, Exception::GENERAL_MOCK); } if($start > $end || $end > $size) { - throw new Exception('Invalid content-range header', 400); + throw new Exception('Invalid content-range header', 400, Exception::GENERAL_MOCK); } if($start === 0 && !empty($id)) { - throw new Exception('First chunked request cannot have id header', 400); + throw new Exception('First chunked request cannot have id header', 400, Exception::GENERAL_MOCK); } if($start !== 0 && $id !== 'newfileid') { - throw new Exception('All chunked request must have id header (except first)', 400); + throw new Exception('All chunked request must have id header (except first)', 400, Exception::GENERAL_MOCK); } if($end !== $size && $end-$start+1 !== 5*1024*1024) { - throw new Exception('Chunk size must be 5MB (except last chunk)', 400); + throw new Exception('Chunk size must be 5MB (except last chunk)', 400, Exception::GENERAL_MOCK); } foreach ($file['size'] as $i => $sz) { if ($end !== $size && $sz !== 5*1024*1024) { - throw new Exception('Wrong chunk size', 400); + throw new Exception('Wrong chunk size', 400, Exception::GENERAL_MOCK); } if($sz > 5*1024*1024) { - throw new Exception('Chunk size must be 5MB or less', 400); + throw new Exception('Chunk size must be 5MB or less', 400, Exception::GENERAL_MOCK); } } if($end !== $size) { @@ -289,19 +290,19 @@ App::post('/v1/mock/tests/general/upload') foreach ($file['name'] as $i => $name) { if ($name !== 'file.png') { - throw new Exception('Wrong file name', 400); + throw new Exception('Wrong file name', 400, Exception::GENERAL_MOCK); } } foreach ($file['size'] as $i => $size) { if ($size !== 38756) { - throw new Exception('Wrong file size', 400); + throw new Exception('Wrong file size', 400, Exception::GENERAL_MOCK); } } foreach ($file['tmp_name'] as $i => $tmpName) { if (\md5(\file_get_contents($tmpName)) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') { - throw new Exception('Wrong file uploaded', 400); + throw new Exception('Wrong file uploaded', 400, Exception::GENERAL_MOCK); } } } @@ -379,7 +380,7 @@ App::get('/v1/mock/tests/general/get-cookie') /** @var Appwrite\Utopia\Request $request */ if ($request->getCookie('cookieName', '') !== 'cookieValue') { - throw new Exception('Missing cookie value', 400); + throw new Exception('Missing cookie value', 400, Exception::GENERAL_MOCK); } }); @@ -414,7 +415,7 @@ App::get('/v1/mock/tests/general/400-error') ->label('sdk.response.model', Response::MODEL_ERROR) ->label('sdk.mock', true) ->action(function () { - throw new Exception('Mock 400 error', 400); + throw new Exception('Mock 400 error', 400, Exception::GENERAL_MOCK); }); App::get('/v1/mock/tests/general/500-error') @@ -430,7 +431,7 @@ App::get('/v1/mock/tests/general/500-error') ->label('sdk.response.model', Response::MODEL_ERROR) ->label('sdk.mock', true) ->action(function () { - throw new Exception('Mock 500 error', 500); + throw new Exception('Mock 500 error', 500, Exception::GENERAL_MOCK); }); App::get('/v1/mock/tests/general/502-error') @@ -489,11 +490,11 @@ App::get('/v1/mock/tests/general/oauth2/token') /** @var Appwrite\Utopia\Response $response */ if ($client_id != '1') { - throw new Exception('Invalid client ID'); + throw new Exception('Invalid client ID', 400, Exception::GENERAL_MOCK); } if ($client_secret != '123456') { - throw new Exception('Invalid client secret'); + throw new Exception('Invalid client secret', 400, Exception::GENERAL_MOCK); } $responseJson = [ @@ -504,18 +505,18 @@ App::get('/v1/mock/tests/general/oauth2/token') if($grantType === 'authorization_code') { if ($code !== 'abcdef') { - throw new Exception('Invalid token'); + throw new Exception('Invalid token', 400, Exception::GENERAL_MOCK); } $response->json($responseJson); } else if($grantType === 'refresh_token') { if ($refreshToken !== 'tuvwxyz') { - throw new Exception('Invalid refresh token'); + throw new Exception('Invalid refresh token', 400, Exception::GENERAL_MOCK); } $response->json($responseJson); } else { - throw new Exception('Invalid grant type'); + throw new Exception('Invalid grant type', 400, Exception::GENERAL_MOCK); } }); @@ -530,7 +531,7 @@ App::get('/v1/mock/tests/general/oauth2/user') /** @var Appwrite\Utopia\Response $response */ if ($token != '123456') { - throw new Exception('Invalid token'); + throw new Exception('Invalid token', 400, Exception::GENERAL_MOCK); } $response->json([ @@ -581,7 +582,7 @@ App::shutdown(function($utopia, $response, $request) { $tests = (\file_exists($path)) ? \json_decode(\file_get_contents($path), true) : []; if (!\is_array($tests)) { - throw new Exception('Failed to read results', 500); + throw new Exception('Failed to read results', 500, Exception::GENERAL_MOCK); } $result[$route->getMethod() . ':' . $route->getPath()] = true; @@ -589,7 +590,7 @@ App::shutdown(function($utopia, $response, $request) { $tests = \array_merge($tests, $result); if (!\file_put_contents($path, \json_encode($tests), LOCK_EX)) { - throw new Exception('Failed to save results', 500); + throw new Exception('Failed to save results', 500, Exception::GENERAL_MOCK); } $response->dynamic(new Document(['result' => $route->getMethod() . ':' . $route->getPath() . ':passed']), Response::MODEL_MOCK); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 71b2ff95d..ed485eff8 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -33,6 +33,7 @@ class Exception extends \Exception /** General */ const GENERAL_UNKNOWN = 'general_unknown'; + const GENERAL_MOCK = 'general_mock'; const GENERAL_ACCESS_FORBIDDEN = 'general_access_forbidden'; const GENERAL_UNKNOWN_ORIGIN = 'general_unknown_origin'; const GENERAL_SERVICE_DISABLED = 'general_service_disabled';