1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

feat: update error codes in the teams API

This commit is contained in:
Christy Jacob 2022-02-06 19:38:15 +04:00
parent 5fc751a9dd
commit c3369dec50
3 changed files with 20 additions and 8 deletions

View file

@ -186,8 +186,8 @@ return [
'description' => 'Failed to delete team from the database.', 'description' => 'Failed to delete team from the database.',
'statusCode' => 500, 'statusCode' => 500,
], ],
Exception::TEAM_INVITATION_ALREADY_EXISTS => [ Exception::TEAM_INVITE_ALREADY_EXISTS => [
'name' => Exception::TEAM_INVITATION_ALREADY_EXISTS, 'name' => Exception::TEAM_INVITE_ALREADY_EXISTS,
'description' => 'The current user already has an invitation to this team.', 'description' => 'The current user already has an invitation to this team.',
'statusCode' => 409, 'statusCode' => 409,
], ],
@ -196,6 +196,16 @@ return [
'description' => 'The requested invitation could not be found.', 'description' => 'The requested invitation could not be found.',
'statusCode' => 409, '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 */ /** Membership */

View file

@ -370,7 +370,7 @@ App::post('/v1/teams/:teamId/memberships')
try { try {
$membership = Authorization::skip(fn() => $dbForProject->createDocument('memberships', $membership)); $membership = Authorization::skip(fn() => $dbForProject->createDocument('memberships', $membership));
} catch (Duplicate $th) { } 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->setAttribute('sum', $team->getAttribute('sum', 0) + 1);
$team = Authorization::skip(fn() => $dbForProject->updateDocument('teams', $team->getId(), $team)); $team = Authorization::skip(fn() => $dbForProject->updateDocument('teams', $team->getId(), $team));
@ -383,7 +383,7 @@ App::post('/v1/teams/:teamId/memberships')
try { try {
$membership = $dbForProject->createDocument('memberships', $membership); $membership = $dbForProject->createDocument('memberships', $membership);
} catch (Duplicate $th) { } 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) { 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)); $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')) { 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')) { if ($userId != $membership->getAttribute('userId')) {
@ -767,7 +767,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId')
} catch (AuthorizationException $exception) { } catch (AuthorizationException $exception) {
throw new Exception('Unauthorized permissions', 401, Exception::USER_UNAUTHORIZED); throw new Exception('Unauthorized permissions', 401, Exception::USER_UNAUTHORIZED);
} catch (\Exception $exception) { } 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', []); $memberships = $user->getAttribute('memberships', []);

View file

@ -55,8 +55,10 @@ class Exception extends \Exception
/** Teams */ /** Teams */
const TEAM_NOT_FOUND = 'team_not_found'; const TEAM_NOT_FOUND = 'team_not_found';
const TEAM_DELETION_FAILED = 'team_deletion_failed'; 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_INVITE_NOT_FOUND = 'team_invite_not_found';
const TEAM_INVALID_SECRET = 'team_invalid_secret';
const TEAM_MEMBERSHIP_MISMATCH = 'team_membership_mismatch';
/** Membership */ /** Membership */
const MEMBERSHIP_NOT_FOUND = 'membership_not_found'; const MEMBERSHIP_NOT_FOUND = 'membership_not_found';