1
0
Fork 0
mirror of synced 2024-06-17 10:14:50 +12:00

feat: update error codes in the teams API

This commit is contained in:
Christy Jacob 2022-02-06 19:17:19 +04:00
parent d756a9b972
commit 5fc751a9dd
3 changed files with 42 additions and 12 deletions

View file

@ -127,6 +127,11 @@ return [
'description' => 'The current user session could not be found.',
'statusCode' => 404,
],
Exception::USER_UNAUTHORIZED => [
'name' => Exception::USER_UNAUTHORIZED,
'description' => 'The current user is not authorized to perform the requested action.',
'statusCode' => 401,
],
Exception::USER_AUTH_METHOD_UNSUPPORTED => [
'name' => Exception::USER_AUTH_METHOD_UNSUPPORTED,
'description' => 'The requested authentication method is either disabled or unsupported.',
@ -176,14 +181,34 @@ return [
'description' => 'Team with the requested ID could not be found.',
'statusCode' => 404,
],
Exception::TEAM_DELETION_FAILED => [
'name' => Exception::TEAM_DELETION_FAILED,
'description' => 'Failed to delete team from the database.',
'statusCode' => 500,
],
Exception::TEAM_INVITATION_ALREADY_EXISTS => [
'name' => Exception::TEAM_INVITATION_ALREADY_EXISTS,
'description' => 'The current user already has an invitation to this team.',
'statusCode' => 409,
],
Exception::TEAM_INVITE_NOT_FOUND => [
'name' => Exception::TEAM_INVITE_NOT_FOUND,
'description' => 'The requested invitation could not be found.',
'statusCode' => 409,
],
/** Membership */
Exception::MEMBERSHIP_NOT_FOUND => [
'name' => Exception::MEMBERSHIP_NOT_FOUND,
'description' => 'Membership with the requested ID could not be found.',
'statusCode' => 404,
],
Exception::MEMBERSHIP_DELETION_FAILED => [
'name' => Exception::MEMBERSHIP_DELETION_FAILED,
'description' => 'Failed to delete membership from the database.',
'statusCode' => 500,
],
/** Avatars */
Exception::AVATAR_SET_NOT_FOUND => [

View file

@ -232,12 +232,12 @@ App::delete('/v1/teams/:teamId')
// TODO delete all members individually from the user object
foreach ($memberships as $membership) {
if (!$dbForProject->deleteDocument('memberships', $membership->getId())) {
throw new Exception('Failed to remove membership for team from DB', 500);
throw new Exception('Failed to remove membership for team from DB', 500, Exception::MEMBERSHIP_DELETION_FAILED);
}
}
if (!$dbForProject->deleteDocument('teams', $teamId)) {
throw new Exception('Failed to remove team from DB', 500);
throw new Exception('Failed to remove team from DB', 500, Exception::TEAM_DELETION_FAILED);
}
$deletes
@ -348,7 +348,7 @@ App::post('/v1/teams/:teamId/memberships')
$isOwner = Authorization::isRole('team:'.$team->getId().'/owner');;
if (!$isOwner && !$isPrivilegedUser && !$isAppUser) { // Not owner, not admin, not app (server)
throw new Exception('User is not allowed to send invitations for this team', 401);
throw new Exception('User is not allowed to send invitations for this team', 401, Exception::USER_UNAUTHORIZED);
}
$secret = Auth::tokenGenerator();
@ -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);
throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITATION_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);
throw new Exception('User has already been invited or is already a member of this team', 409, Exception::TEAM_INVITATION_ALREADY_EXISTS);
}
}
@ -568,7 +568,7 @@ App::patch('/v1/teams/:teamId/memberships/:membershipId')
$isOwner = Authorization::isRole('team:'.$team->getId().'/owner');;
if (!$isOwner && !$isPrivilegedUser && !$isAppUser) { // Not owner, not admin, not app (server)
throw new Exception('User is not allowed to modify roles', 401);
throw new Exception('User is not allowed to modify roles', 401, Exception::USER_UNAUTHORIZED);
}
// Update the roles
@ -743,7 +743,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId')
$membership = $dbForProject->getDocument('memberships', $membershipId);
if ($membership->isEmpty()) {
throw new Exception('Invite not found', 404);
throw new Exception('Invite not found', 404, Exception::TEAM_INVITE_NOT_FOUND);
}
if ($membership->getAttribute('teamId') !== $teamId) {
@ -753,7 +753,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId')
$user = $dbForProject->getDocument('users', $membership->getAttribute('userId'));
if ($user->isEmpty()) {
throw new Exception('User not found', 404);
throw new Exception('User not found', 404, Exception::USER_NOT_FOUND);
}
$team = $dbForProject->getDocument('teams', $teamId);
@ -765,7 +765,7 @@ App::delete('/v1/teams/:teamId/memberships/:membershipId')
try {
$dbForProject->deleteDocument('memberships', $membership->getId());
} catch (AuthorizationException $exception) {
throw new Exception('Unauthorized permissions', 401, Exception::UNAUTHORIZED_SCOPE);
throw new Exception('Unauthorized permissions', 401, Exception::USER_UNAUTHORIZED);
} catch (\Exception $exception) {
throw new Exception('Failed to remove membership from DB', 500);
}

View file

@ -40,6 +40,7 @@ class Exception extends \Exception
const USER_IP_NOT_WHITELISTED = 'user_ip_not_whitelisted';
const USER_SESSION_ALREADY_EXISTS = 'user_session_already_exists';
const USER_SESSION_NOT_FOUND = 'user_session_not_found';
const USER_UNAUTHORIZED = 'user_unauthorized';
const USER_ANONYMOUS_CONSOLE_PROHIBITED = 'user_anonymous_console_prohibited';
/** OAuth **/
@ -52,10 +53,14 @@ class Exception extends \Exception
const OAUTH_MISSING_USER_ID = 'oauth_missing_user_id';
/** Teams */
const TEAM_NOT_FOUND = 'team_not_found';
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_NOT_FOUND = 'team_invite_not_found';
/** Membership */
const MEMBERSHIP_NOT_FOUND = 'membership_not_found';
const MEMBERSHIP_NOT_FOUND = 'membership_not_found';
const MEMBERSHIP_DELETION_FAILED = 'membership_deletion_failed';
/** Avatars */
const AVATAR_SET_NOT_FOUND = 'avatar_set_not_found';