From 6c65a48368f16cd40d3210a850cedc1b9d05fc39 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 21 Dec 2023 11:00:19 +0200 Subject: [PATCH] throwing 409 on duplicate document --- app/config/errors.php | 5 +++++ app/controllers/api/vcs.php | 9 +++++++-- src/Appwrite/Extend/Exception.php | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index c0628920d9..a7b810d2c1 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -200,6 +200,11 @@ return [ 'description' => 'The identity could not be found. Please sign in with OAuth provider to create identity first.', 'code' => 404, ], + Exception::USER_IDENTITY_ALREADY_EXISTS => [ + 'name' => Exception::USER_IDENTITY_ALREADY_EXISTS, + 'description' => 'The identity already exists.', + 'code' => 409, + ], Exception::USER_UNAUTHORIZED => [ 'name' => Exception::USER_UNAUTHORIZED, 'description' => 'The current user is not authorized to perform the requested action.', diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 1b0c993e11..d9a0d797b8 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -4,6 +4,7 @@ use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Utopia\App; use Appwrite\Event\Build; use Appwrite\Event\Delete; +use Utopia\Database\Exception\Duplicate; use Utopia\Validator\Host; use Utopia\Database\Database; use Utopia\Database\Document; @@ -368,7 +369,8 @@ App::get('/v1/vcs/github/callback') $dbForConsole->updateDocument('identities', $identity->getId(), $identity); } else { - $identity = $dbForConsole->createDocument('identities', new Document([ + try { + $dbForConsole->createDocument('identities', new Document([ '$id' => ID::unique(), '$permissions' => [ Permission::read(Role::any()), @@ -383,7 +385,10 @@ App::get('/v1/vcs/github/callback') 'providerAccessToken' => $accessToken, 'providerRefreshToken' => $refreshToken, 'providerAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), (int)$accessTokenExpiry), - ])); + ])); + } catch (Duplicate) { + throw new Exception(Exception::USER_IDENTITY_ALREADY_EXISTS); + } } } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 6449ffd93a..879ef31fe9 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -76,6 +76,7 @@ class Exception extends \Exception public const USER_PASSWORD_MISMATCH = 'user_password_mismatch'; public const USER_SESSION_NOT_FOUND = 'user_session_not_found'; public const USER_IDENTITY_NOT_FOUND = 'user_identity_not_found'; + public const USER_IDENTITY_ALREADY_EXISTS = 'user_identity_already_exists'; public const USER_UNAUTHORIZED = 'user_unauthorized'; public const USER_AUTH_METHOD_UNSUPPORTED = 'user_auth_method_unsupported'; public const USER_PHONE_ALREADY_EXISTS = 'user_phone_already_exists';