From b16faed83a5cf42b8ab63afd59f22bd76ef5722a Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 6 Feb 2022 16:49:01 +0400 Subject: [PATCH] feat: update error codes in users API --- app/config/errors.php | 174 ++++++++++++++---------------- app/controllers/api/users.php | 34 +++--- app/controllers/general.php | 18 ++-- app/controllers/shared/api.php | 18 ++-- src/Appwrite/Extend/Exception.php | 64 +++++------ 5 files changed, 142 insertions(+), 166 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 8b568de0a..b85841aa6 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -7,163 +7,145 @@ use Appwrite\Extend\Exception; return [ - Exception::TYPE_PROJECT_NOT_FOUND => [ - 'name' => Exception::TYPE_PROJECT_NOT_FOUND, - 'description' => 'The requested project could not be found. Please check the value of the X-Appwrite-Project header to ensure the correct project ID is being used.', - 'statusCode' => 404, - ], - Exception::TYPE_PROJECT_UNKNOWN => [ - 'name' => Exception::TYPE_PROJECT_UNKNOWN, - 'description' => 'The project ID is either missing or not valid. Please check the value of the X-Appwrite-Project header to ensure the correct project ID is being used.', - 'statusCode' => 400, - ], - Exception::TYPE_INVALID_ORIGIN => [ - 'name' => Exception::TYPE_INVALID_ORIGIN, + /** General Errors */ + Exception::UNKNOWN_ORIGIN => [ + 'name' => Exception::UNKNOWN_ORIGIN, 'description' => 'The request originated from a non-whitelisted origin. If you trust this origin, please add it as a platform in the Appwrite console.', 'statusCode' => 403, ], - Exception::TYPE_SERVICE_DISABLED => [ - 'name' => Exception::TYPE_SERVICE_DISABLED, + Exception::SERVICE_DISABLED => [ + 'name' => Exception::SERVICE_DISABLED, 'description' => 'The requested service is disabled. You can enable/disable a service from the Appwrite console or by contacting the project owner.', 'statusCode' => 503, ], - Exception::TYPE_UNAUTHORIZED_SCOPE => [ - 'name' => Exception::TYPE_UNAUTHORIZED_SCOPE, - 'description' => 'The current user is not authorized to access the requested resource.', + Exception::UNAUTHORIZED_SCOPE => [ + 'name' => Exception::UNAUTHORIZED_SCOPE, + 'description' => 'The current user or API key does not have the required scopes to access the requested resource.', 'statusCode' => 401, ], - Exception::TYPE_PASSWORD_RESET_REQUIRED => [ - 'name' => Exception::TYPE_PASSWORD_RESET_REQUIRED, - 'description' => 'The current user requires a password reset.', - 'statusCode' => 412, - ], - Exception::TYPE_STORAGE_ERROR => [ - 'name' => Exception::TYPE_STORAGE_ERROR, + Exception::STORAGE_ERROR => [ + 'name' => Exception::STORAGE_ERROR, 'description' => 'Storage error', 'statusCode' => 500, ], - Exception::TYPE_RATE_LIMIT_EXCEEDED => [ - 'name' => Exception::TYPE_RATE_LIMIT_EXCEEDED, - 'description' => 'Rate limit for the current endpoint has been exceeded. ', + Exception::RATE_LIMIT_EXCEEDED => [ + 'name' => Exception::RATE_LIMIT_EXCEEDED, + 'description' => 'Rate limit for the current endpoint has been exceeded. Please try again after some time.', 'statusCode' => 429, ], - Exception::TYPE_SMTP_DISABLED => [ - 'name' => Exception::TYPE_SMTP_DISABLED, + Exception::SMTP_DISABLED => [ + 'name' => Exception::SMTP_DISABLED, 'description' => 'SMTP is disabled on your Appwrite instance. Please contact your project ', 'statusCode' => 503, ], - Exception::TYPE_EMAIL_NOT_WHITELISTED => [ - 'name' => Exception::TYPE_EMAIL_NOT_WHITELISTED, - 'description' => 'The user\'s email is not part of the whitelist. Please check the _APP_CONSOLE_WHITELIST_EMAILS environment variable of your Appwrite server.', - 'statusCode' => 401, - ], - Exception::TYPE_IP_NOT_WHITELISTED => [ - 'name' => Exception::TYPE_IP_NOT_WHITELISTED, - 'description' => 'The user\'s IP address is not part of the whitelist. Please check the _APP_CONSOLE_WHITELIST_IPS environment variable of your Appwrite server.', - 'statusCode' => 401, - ], - Exception::TYPE_INVALID_CREDENTIALS => [ - 'name' => Exception::TYPE_INVALID_CREDENTIALS, - 'description' => 'Invalid credentials. Please check the email and password.', - 'statusCode' => 401, - ], - Exception::TYPE_INVALID_TOKEN => [ - 'name' => Exception::TYPE_INVALID_TOKEN, - 'description' => 'The used token is invalid.', - 'statusCode' => 401, - ], - Exception::TYPE_JWT_VERIFICATION_FAILED => [ - 'name' => Exception::TYPE_JWT_VERIFICATION_FAILED, - 'description' => 'Invalid refresh token', - 'statusCode' => 403, - ], - Exception::TYPE_ANONYMOUS_CONSOLE_USER => [ - 'name' => Exception::TYPE_ANONYMOUS_CONSOLE_USER, - 'description' => 'Anonymous session cannot be created for the console project.', - 'statusCode' => 401, - ], - Exception::TYPE_SESSION_NOT_FOUND => [ - 'name' => Exception::TYPE_SESSION_NOT_FOUND, - 'description' => 'No valid session found.', + + /** Project Errors */ + Exception::PROJECT_NOT_FOUND => [ + 'name' => Exception::PROJECT_NOT_FOUND, + 'description' => 'The requested project could not be found. Please check the value of the X-Appwrite-Project header to ensure the correct project ID is being used.', 'statusCode' => 404, ], - Exception::TYPE_SESSION_ALREADY_EXISTS => [ - 'name' => Exception::TYPE_SESSION_ALREADY_EXISTS, - 'description' => 'Cannot create anonymous session when there is an active session.', - 'statusCode' => 401, + Exception::PROJECT_UNKNOWN => [ + 'name' => Exception::PROJECT_UNKNOWN, + 'description' => 'The project ID is either missing or not valid. Please check the value of the X-Appwrite-Project header to ensure the correct project ID is being used.', + 'statusCode' => 400, ], - Exception::TYPE_USER_LIMIT_EXCEEDED => [ - 'name' => Exception::TYPE_USER_LIMIT_EXCEEDED, + + /** User Errors */ + Exception::USER_COUNT_EXCEEDED => [ + 'name' => Exception::USER_COUNT_EXCEEDED, 'description' => 'The current project has exceeded the maximum number of users. Please check your user limit in the Appwrite console.', 'statusCode' => 501, ], - Exception::TYPE_USER_ALREADY_EXISTS => [ - 'name' => Exception::TYPE_USER_ALREADY_EXISTS, + Exception::USER_EMAIL_NOT_WHITELISTED => [ + 'name' => Exception::USER_EMAIL_NOT_WHITELISTED, + 'description' => 'The user\'s email is not part of the whitelist. Please check the _APP_CONSOLE_WHITELIST_EMAILS environment variable of your Appwrite server.', + 'statusCode' => 401, + ], + Exception::USER_PASSWORD_RESET_REQUIRED => [ + 'name' => Exception::USER_PASSWORD_RESET_REQUIRED, + 'description' => 'The current user requires a password reset.', + 'statusCode' => 412, + ], + Exception::USER_IP_NOT_WHITELISTED => [ + 'name' => Exception::USER_IP_NOT_WHITELISTED, + 'description' => 'The user\'s IP address is not part of the whitelist. Please check the _APP_CONSOLE_WHITELIST_IPS environment variable of your Appwrite server.', + 'statusCode' => 401, + ], + Exception::USER_INVALID_CREDENTIALS => [ + 'name' => Exception::USER_INVALID_CREDENTIALS, + 'description' => 'Invalid credentials. Please check the email and password.', + 'statusCode' => 401, + ], + Exception::USER_ALREADY_EXISTS => [ + 'name' => Exception::USER_ALREADY_EXISTS, 'description' => 'A user with the same email ID already exists in your project.', 'statusCode' => 409, ], - Exception::TYPE_USER_BLOCKED => [ - 'name' => Exception::TYPE_USER_BLOCKED, + Exception::USER_BLOCKED => [ + 'name' => Exception::USER_BLOCKED, 'description' => 'The current user has been blocked. Please contact the project administrator for more information.', 'statusCode' => 401, ], - Exception::TYPE_USER_CREATION_FAILED => [ - 'name' => Exception::TYPE_USER_CREATION_FAILED, + Exception::USER_CREATION_FAILED => [ + 'name' => Exception::USER_CREATION_FAILED, 'description' => 'There was an internal server error while creating the user.', 'statusCode' => 500, ], - Exception::TYPE_USER_NOT_FOUND => [ - 'name' => Exception::TYPE_USER_NOT_FOUND, + Exception::USER_NOT_FOUND => [ + 'name' => Exception::USER_NOT_FOUND, 'description' => 'User with the requested ID could not be found.', 'statusCode' => 404, ], - Exception::TYPE_EMAIL_ALREADY_EXISTS => [ - 'name' => Exception::TYPE_EMAIL_ALREADY_EXISTS, + Exception::USER_EMAIL_ALREADY_EXISTS => [ + 'name' => Exception::USER_EMAIL_ALREADY_EXISTS, 'description' => 'Another user with the same email already exists in the current project.', 'statusCode' => 409, ], - Exception::TYPE_PASSWORD_MISMATCH => [ - 'name' => Exception::TYPE_PASSWORD_MISMATCH, + Exception::USER_PASSWORD_MISMATCH => [ + 'name' => Exception::USER_PASSWORD_MISMATCH, 'description' => 'Passwords do not match. Please recheck.', 'statusCode' => 400, ], - Exception::TYPE_AUTH_METHOD_UNSUPPORTED => [ - 'name' => Exception::TYPE_AUTH_METHOD_UNSUPPORTED, + Exception::USER_AUTH_METHOD_UNSUPPORTED => [ + 'name' => Exception::USER_AUTH_METHOD_UNSUPPORTED, 'description' => 'The requested authentication method is either disabled or unsupported.', 'statusCode' => 501, ], - Exception::TYPE_PROVIDER_DISABLED => [ - 'name' => Exception::TYPE_PROVIDER_DISABLED, + + /** OAuth Errors */ + Exception::OAUTH_PROVIDER_DISABLED => [ + 'name' => Exception::OAUTH_PROVIDER_DISABLED, 'description' => 'The chosen OAuth provider is disabled. Please contact your project administrator for more information.', 'statusCode' => 412, ], - Exception::TYPE_PROVIDER_UNSUPPORTED => [ - 'name' => Exception::TYPE_PROVIDER_UNSUPPORTED, + Exception::OAUTH_PROVIDER_UNSUPPORTED => [ + 'name' => Exception::OAUTH_PROVIDER_UNSUPPORTED, 'description' => 'The chosen OAuth provider is unsupported.', 'statusCode' => 501, ], - Exception::TYPE_INVALID_LOGIN_STATE_PARAMS => [ - 'name' => Exception::TYPE_INVALID_LOGIN_STATE_PARAMS, + Exception::OAUTH_INVALID_LOGIN_STATE_PARAMS => [ + 'name' => Exception::OAUTH_INVALID_LOGIN_STATE_PARAMS, 'description' => 'Failed to parse the login state params from the OAuth provider.', 'statusCode' => 500, ], - Exception::TYPE_INVALID_SUCCESS_URL => [ - 'name' => Exception::TYPE_INVALID_SUCCESS_URL, + Exception::OAUTH_INVALID_SUCCESS_URL => [ + 'name' => Exception::OAUTH_INVALID_SUCCESS_URL, 'description' => 'Invalid URL received for OAuth success redirect.', 'statusCode' => 400, ], - Exception::TYPE_INVALID_FAILURE_URL => [ - 'name' => Exception::TYPE_INVALID_FAILURE_URL, + Exception::OAUTH_INVALID_FAILURE_URL => [ + 'name' => Exception::OAUTH_INVALID_FAILURE_URL, 'description' => 'Invalid URL received for OAuth failure redirect.', 'statusCode' => 400, ], - Exception::TYPE_OAUTH_ACCESS_TOKEN_FAILED => [ - 'name' => Exception::TYPE_OAUTH_ACCESS_TOKEN_FAILED, + Exception::OAUTH_ACCESS_TOKEN_FAILED => [ + 'name' => Exception::OAUTH_ACCESS_TOKEN_FAILED, 'description' => 'Failed to obtain access token from the OAuth provider.', 'statusCode' => 500, ], - Exception::TYPE_MISSING_USER_ID => [ - 'name' => Exception::TYPE_MISSING_USER_ID, + Exception::OAUTH_MISSING_USER_ID => [ + 'name' => Exception::OAUTH_MISSING_USER_ID, 'description' => 'Failed to obtain user id from the OAuth provider.', 'statusCode' => 400, ] diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index ac030f7e7..3c89e4614 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -4,7 +4,7 @@ use Appwrite\Auth\Auth; use Appwrite\Auth\Validator\Password; use Appwrite\Utopia\Response; use Utopia\App; -use Utopia\Exception; +use Appwrite\Extend\Exception; use Utopia\Validator\Assoc; use Utopia\Validator\WhiteList; use Appwrite\Network\Validator\Email; @@ -70,7 +70,7 @@ App::post('/v1/users') 'deleted' => false ])); } catch (Duplicate $th) { - throw new Exception('Account already exists', 409); + throw new Exception('Account already exists', 409, Exception::USER_ALREADY_EXISTS); } $usage @@ -110,7 +110,7 @@ App::get('/v1/users') $cursorUser = $dbForProject->getDocument('users', $cursor); if ($cursorUser->isEmpty()) { - throw new Exception("User '{$cursor}' for the 'cursor' value not found.", 400); + throw new Exception("User '{$cursor}' for the 'cursor' value not found.", 404, Exception::USER_NOT_FOUND); } } @@ -155,7 +155,7 @@ App::get('/v1/users/:userId') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $usage @@ -187,7 +187,7 @@ App::get('/v1/users/:userId/prefs') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $prefs = $user->getAttribute('prefs', new \stdClass()); @@ -223,7 +223,7 @@ App::get('/v1/users/:userId/sessions') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $sessions = $user->getAttribute('sessions', []); @@ -277,7 +277,7 @@ App::get('/v1/users/:userId/logs') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $audit = new Audit($dbForProject); @@ -377,7 +377,7 @@ App::patch('/v1/users/:userId/status') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $user = $dbForProject->updateDocument('users', $user->getId(), $user->setAttribute('status', (bool) $status)); @@ -413,7 +413,7 @@ App::patch('/v1/users/:userId/verification') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $user = $dbForProject->updateDocument('users', $user->getId(), $user->setAttribute('emailVerification', $emailVerification)); @@ -449,7 +449,7 @@ App::patch('/v1/users/:userId/name') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $user = $dbForProject->updateDocument('users', $user->getId(), $user->setAttribute('name', $name)); @@ -488,7 +488,7 @@ App::patch('/v1/users/:userId/password') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $user @@ -531,7 +531,7 @@ App::patch('/v1/users/:userId/email') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $isAnonymousUser = is_null($user->getAttribute('email')) && is_null($user->getAttribute('password')); // Check if request is from an anonymous account for converting @@ -544,7 +544,7 @@ App::patch('/v1/users/:userId/email') try { $user = $dbForProject->updateDocument('users', $user->getId(), $user->setAttribute('email', $email)); } catch(Duplicate $th) { - throw new Exception('Email already exists', 409); + throw new Exception('Email already exists', 409, Exception::USER_EMAIL_ALREADY_EXISTS); } $audits @@ -581,7 +581,7 @@ App::patch('/v1/users/:userId/prefs') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $user = $dbForProject->updateDocument('users', $user->getId(), $user->setAttribute('prefs', $prefs)); @@ -618,7 +618,7 @@ App::delete('/v1/users/:userId/sessions/:sessionId') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $sessions = $user->getAttribute('sessions', []); @@ -673,7 +673,7 @@ App::delete('/v1/users/:userId/sessions') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } $sessions = $user->getAttribute('sessions', []); @@ -722,7 +722,7 @@ App::delete('/v1/users/:userId') $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty() || $user->getAttribute('deleted')) { - throw new Exception('User not found', 404); + throw new Exception('User not found', 404, Exception::USER_NOT_FOUND); } /** diff --git a/app/controllers/general.php b/app/controllers/general.php index 7f82c36a3..dba5caed9 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -8,7 +8,7 @@ use Utopia\Logger\Log\User; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Utopia\View; -use Utopia\Exception; +use Appwrite\Extend\Exception; use Utopia\Config\Config; use Utopia\Domains\Domain; use Appwrite\Auth\Auth; @@ -108,11 +108,11 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons } if ($project->isEmpty()) { - throw new Exception('Project not found', 404); + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); } if (!empty($route->getLabel('sdk.auth', [])) && $project->isEmpty() && ($route->getLabel('scope', '') !== 'public')) { - throw new Exception('Missing or unknown project ID', 400); + throw new Exception('Missing or unknown project ID', 400, Exception::PROJECT_UNKNOWN); } $referrer = $request->getReferer(); @@ -214,7 +214,7 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons && \in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE]) && $route->getLabel('origin', false) !== '*' && empty($request->getHeader('x-appwrite-key', ''))) { - throw new Exception($originValidator->getDescription(), 403); + throw new Exception($originValidator->getDescription(), 403, Exception::UNKNOWN_ORIGIN); } /* @@ -283,24 +283,24 @@ App::init(function ($utopia, $request, $response, $console, $project, $dbForCons if(array_key_exists($service, $project->getAttribute('services',[])) && !$project->getAttribute('services',[])[$service] && !Auth::isPrivilegedUser(Authorization::getRoles())) { - throw new Exception('Service is disabled', 503); + throw new Exception('Service is disabled', 503, Exception::SERVICE_DISABLED); } } if (!\in_array($scope, $scopes)) { if ($project->isEmpty()) { // Check if permission is denied because project is missing - throw new Exception('Project not found', 404); + throw new Exception('Project not found', 404, Exception::PROJECT_NOT_FOUND); } - throw new Exception($user->getAttribute('email', 'User').' (role: '.\strtolower($roles[$role]['label']).') missing scope ('.$scope.')', 401); + throw new Exception($user->getAttribute('email', 'User').' (role: '.\strtolower($roles[$role]['label']).') missing scope ('.$scope.')', 401, Exception::UNAUTHORIZED_SCOPE); } if (false === $user->getAttribute('status')) { // Account is blocked - throw new Exception('Invalid credentials. User is blocked', 401); + throw new Exception('Invalid credentials. User is blocked', 401, Exception::USER_BLOCKED); } if ($user->getAttribute('reset')) { - throw new Exception('Password reset is required', 412); + throw new Exception('Password reset is required', 412, Exception::USER_PASSWORD_RESET_REQUIRED); } }, ['utopia', 'request', 'response', 'console', 'project', 'dbForConsole', 'user', 'locale', 'clients']); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 47b1b9a4d..f2abfc90f 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -4,7 +4,7 @@ use Appwrite\Auth\Auth; use Appwrite\Database\Validator\Authorization; use Appwrite\Messaging\Adapter\Realtime; use Utopia\App; -use Utopia\Exception; +use Appwrite\Extend\Exception; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapters\TimeLimit; use Utopia\Database\Document; @@ -32,7 +32,7 @@ App::init(function ($utopia, $request, $response, $project, $user, $events, $aud $route = $utopia->match($request); if ($project->isEmpty() && $route->getLabel('abuse-limit', 0) > 0) { // Abuse limit requires an active project scope - throw new Exception('Missing or unknown project ID', 400); + throw new Exception('Missing or unknown project ID', 400, Exception::PROJECT_UNKNOWN); } /* @@ -81,7 +81,7 @@ App::init(function ($utopia, $request, $response, $project, $user, $events, $aud && App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled') // Abuse is not disabled && (!$isAppUser && !$isPrivilegedUser)) // User is not an admin or API key { - throw new Exception('Too many requests', 429); + throw new Exception('Too many requests', 429, Exception::RATE_LIMIT_EXCEEDED); } } @@ -150,36 +150,36 @@ App::init(function ($utopia, $request, $project) { switch ($route->getLabel('auth.type', '')) { case 'emailPassword': if(($auths['emailPassword'] ?? true) === false) { - throw new Exception('Email / Password authentication is disabled for this project', 501); + throw new Exception('Email / Password authentication is disabled for this project', 501, Exception::USER_AUTH_METHOD_UNSUPPORTED); } break; case 'magic-url': if($project->getAttribute('usersAuthMagicURL', true) === false) { - throw new Exception('Magic URL authentication is disabled for this project', 501); + throw new Exception('Magic URL authentication is disabled for this project', 501, Exception::USER_AUTH_METHOD_UNSUPPORTED); } break; case 'anonymous': if(($auths['anonymous'] ?? true) === false) { - throw new Exception('Anonymous authentication is disabled for this project', 501); + throw new Exception('Anonymous authentication is disabled for this project', 501, Exception::USER_AUTH_METHOD_UNSUPPORTED); } break; case 'invites': if(($auths['invites'] ?? true) === false) { - throw new Exception('Invites authentication is disabled for this project', 501); + throw new Exception('Invites authentication is disabled for this project', 501, Exception::USER_AUTH_METHOD_UNSUPPORTED); } break; case 'jwt': if(($auths['JWT'] ?? true) === false) { - throw new Exception('JWT authentication is disabled for this project', 501); + throw new Exception('JWT authentication is disabled for this project', 501, Exception::USER_AUTH_METHOD_UNSUPPORTED); } break; default: - throw new Exception('Unsupported authentication route'); + throw new Exception('Unsupported authentication route', Exception::USER_AUTH_METHOD_UNSUPPORTED); break; } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index f96621049..2d39bdf28 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -8,42 +8,10 @@ class Exception extends \Exception { /** * Error Codes - */ - const TYPE_NONE = ''; - - /** API */ - const TYPE_PROJECT_NOT_FOUND = 'project_not_found'; - const TYPE_PROJECT_UNKNOWN = 'project_unknown'; - const TYPE_INVALID_ORIGIN = 'invalid_origin'; - const TYPE_SERVICE_DISABLED = 'service_disabled'; - const TYPE_UNAUTHORIZED_SCOPE = 'unauthorized_scope'; - const TYPE_PASSWORD_RESET_REQUIRED = 'password_reset_required'; - const TYPE_STORAGE_ERROR = 'storage_error'; - const TYPE_RATE_LIMIT_EXCEEDED = 'rate_limit_exceeded'; - const TYPE_SMTP_DISABLED = 'smtp_disabled'; - - /** Users **/ - const TYPE_EMAIL_NOT_WHITELISTED = 'email_not_whitelisted'; - const TYPE_IP_NOT_WHITELISTED = 'ip_not_whitelisted'; - const TYPE_INVALID_TOKEN = 'invalid_token'; - const TYPE_JWT_VERIFICATION_FAILED = 'jwt_verification_failed'; - const TYPE_ANONYMOUS_CONSOLE_USER = 'anonymous_console_user'; - const TYPE_SESSION_NOT_FOUND = 'session_not_found'; - const TYPE_SESSION_ALREADY_EXISTS = 'session_already_exists'; - - /** OAuth **/ - const TYPE_PROVIDER_DISABLED = 'provider_disabled'; - const TYPE_PROVIDER_UNSUPPORTED = 'provider_unsupported'; - const TYPE_INVALID_LOGIN_STATE_PARAMS = 'invalid_login_state_params'; - const TYPE_INVALID_SUCCESS_URL = 'invalid_success_url'; - const TYPE_INVALID_FAILURE_URL = 'invalid_failure_url'; - const TYPE_OAUTH_ACCESS_TOKEN_FAILED = 'oauth_access_token_failed'; - const TYPE_MISSING_USER_ID = 'missing_user_id'; - - - /** + * * Naming the error types based on the following convention * _ + * * Appwrite has the follwing entities: * - Users * - Projects @@ -66,10 +34,36 @@ class Exception extends \Exception const USER_EMAIL_ALREADY_EXISTS = 'user_email_already_exists'; const USER_PASSWORD_MISMATCH = 'user_password_mismatch'; const USER_AUTH_METHOD_UNSUPPORTED = 'user_auth_method_unsupported'; + const USER_PASSWORD_RESET_REQUIRED = 'user_password_reset_required'; + const USER_EMAIL_NOT_WHITELISTED = 'user_email_not_whitelisted'; + const USER_IP_NOT_WHITELISTED = 'user_ip_not_whitelisted'; + + /** OAuth **/ + const OAUTH_PROVIDER_DISABLED = 'oauth_provider_disabled'; + const OAUTH_PROVIDER_UNSUPPORTED = 'oauth_provider_unsupported'; + const OAUTH_INVALID_LOGIN_STATE_PARAMS = 'oauth_invalid_login_state_params'; + const OAUTH_INVALID_SUCCESS_URL = 'oauth_invalid_success_url'; + const OAUTH_INVALID_FAILURE_URL = 'oauth_invalid_failure_url'; + const OAUTH_ACCESS_TOKEN_FAILED = 'oauth_access_token_failed'; + const OAUTH_MISSING_USER_ID = 'oauth_missing_user_id'; + + /** Projects */ + const PROJECT_NOT_FOUND = 'project_not_found'; + const PROJECT_UNKNOWN = 'project_unknown'; + + /** API */ + const UNKNOWN = 'unknown'; + const UNKNOWN_ORIGIN = 'unknown_origin'; + const SERVICE_DISABLED = 'service_disabled'; + const UNAUTHORIZED_SCOPE = 'unauthorized_scope'; + const STORAGE_ERROR = 'storage_error'; + const RATE_LIMIT_EXCEEDED = 'rate_limit_exceeded'; + const SMTP_DISABLED = 'smtp_disabled'; + private $errorCode = ''; - public function __construct(string $message, int $code = 0, string $errorCode = Exception::TYPE_NONE, \Throwable $previous = null) + public function __construct(string $message, int $code = 0, string $errorCode = Exception::UNKNOWN, \Throwable $previous = null) { $this->errorCode = $errorCode;