From c3369dec5056c0ec5f7444399a5b4ca8650de341 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 6 Feb 2022 19:38:15 +0400 Subject: [PATCH] feat: update error codes in the teams API --- app/config/errors.php | 14 ++++++++++++-- app/controllers/api/teams.php | 10 +++++----- src/Appwrite/Extend/Exception.php | 4 +++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 5b90bfa17..547356b2c 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -186,8 +186,8 @@ return [ 'description' => 'Failed to delete team from the database.', 'statusCode' => 500, ], - Exception::TEAM_INVITATION_ALREADY_EXISTS => [ - 'name' => Exception::TEAM_INVITATION_ALREADY_EXISTS, + Exception::TEAM_INVITE_ALREADY_EXISTS => [ + 'name' => Exception::TEAM_INVITE_ALREADY_EXISTS, 'description' => 'The current user already has an invitation to this team.', 'statusCode' => 409, ], @@ -196,6 +196,16 @@ return [ 'description' => 'The requested invitation could not be found.', 'statusCode' => 409, ], + Exception::TEAM_INVALID_SECRET => [ + 'name' => Exception::TEAM_INVALID_SECRET, + 'description' => 'The team invitation secret is invalid.', + 'statusCode' => 401, + ], + Exception::TEAM_MEMBERSHIP_MISMATCH => [ + 'name' => Exception::TEAM_MEMBERSHIP_MISMATCH, + 'description' => 'The membership ID does not belong to the team ID.', + 'statusCode' => 404, + ], /** Membership */ diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index bc38a28bd..c3fc90c05 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -370,7 +370,7 @@ App::post('/v1/teams/:teamId/memberships') try { $membership = Authorization::skip(fn() => $dbForProject->createDocument('memberships', $membership)); } catch (Duplicate $th) { - throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITATION_ALREADY_EXISTS); + throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITE_ALREADY_EXISTS); } $team->setAttribute('sum', $team->getAttribute('sum', 0) + 1); $team = Authorization::skip(fn() => $dbForProject->updateDocument('teams', $team->getId(), $team)); @@ -383,7 +383,7 @@ App::post('/v1/teams/:teamId/memberships') try { $membership = $dbForProject->createDocument('memberships', $membership); } catch (Duplicate $th) { - throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITATION_ALREADY_EXISTS); + throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITE_ALREADY_EXISTS); } } @@ -625,7 +625,7 @@ App::patch('/v1/teams/:teamId/memberships/:membershipId/status') } if ($membership->getAttribute('teamId') !== $teamId) { - throw new Exception('Team IDs don\'t match', 404); + throw new Exception('Team IDs don\'t match', 404, Exception::TEAM_MEMBERSHIP_MISMATCH); } $team = Authorization::skip(fn() => $dbForProject->getDocument('teams', $teamId)); @@ -635,7 +635,7 @@ App::patch('/v1/teams/:teamId/memberships/:membershipId/status') } if (Auth::hash($secret) !== $membership->getAttribute('secret')) { - throw new Exception('Secret key not valid', 401); + throw new Exception('Secret key not valid', 401, Exception::TEAM_INVALID_SECRET); } if ($userId != $membership->getAttribute('userId')) { @@ -767,7 +767,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId') } catch (AuthorizationException $exception) { throw new Exception('Unauthorized permissions', 401, Exception::USER_UNAUTHORIZED); } catch (\Exception $exception) { - throw new Exception('Failed to remove membership from DB', 500); + throw new Exception('Failed to remove membership from DB', 500, Exception::MEMBERSHIP_DELETION_FAILED); } $memberships = $user->getAttribute('memberships', []); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index d102c7a3f..031fe9ec7 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -55,8 +55,10 @@ class Exception extends \Exception /** Teams */ const TEAM_NOT_FOUND = 'team_not_found'; const TEAM_DELETION_FAILED = 'team_deletion_failed'; - const TEAM_INVITATION_ALREADY_EXISTS = 'team_invitation_already_exists'; + const TEAM_INVITE_ALREADY_EXISTS = 'team_invite_already_exists'; const TEAM_INVITE_NOT_FOUND = 'team_invite_not_found'; + const TEAM_INVALID_SECRET = 'team_invalid_secret'; + const TEAM_MEMBERSHIP_MISMATCH = 'team_membership_mismatch'; /** Membership */ const MEMBERSHIP_NOT_FOUND = 'membership_not_found';