Skip to content

Commit 2b4e17c

Browse files
committed
Replacing DBAL exceptions with appropriate API exceptions in external attributes endpoints.
1 parent 88a1a65 commit 2b4e17c

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

app/V1Module/presenters/GroupExternalAttributesPresenter.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\V1Module\Presenters;
44

5+
use App\Exceptions\BadRequestException;
56
use App\Helpers\MetaFormats\Attributes\Post;
67
use App\Helpers\MetaFormats\Attributes\Query;
78
use App\Helpers\MetaFormats\Attributes\Path;
@@ -16,6 +17,7 @@
1617
use App\Model\Entity\GroupExternalAttribute;
1718
use App\Model\View\GroupViewFactory;
1819
use App\Security\ACL\IGroupPermissions;
20+
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
1921

2022
/**
2123
* Additional attributes used by 3rd parties to keep relations between groups and entities in external systems.
@@ -106,7 +108,7 @@ public function checkAdd()
106108
#[Post("service", new VString(1, 32), "Identifier of the external service creating the attribute", required: true)]
107109
#[Post("key", new VString(1, 32), "Key of the attribute (must be valid identifier)", required: true)]
108110
#[Post("value", new VString(0, 255), "Value of the attribute (arbitrary string)", required: true)]
109-
#[Path("groupId", new VString(), required: true)]
111+
#[Path("groupId", new VUuid(), required: true)]
110112
public function actionAdd(string $groupId)
111113
{
112114
$group = $this->groups->findOrThrow($groupId);
@@ -115,8 +117,13 @@ public function actionAdd(string $groupId)
115117
$service = $req->getPost("service");
116118
$key = $req->getPost("key");
117119
$value = $req->getPost("value");
118-
$attribute = new GroupExternalAttribute($group, $service, $key, $value);
119-
$this->groupExternalAttributes->persist($attribute);
120+
121+
try {
122+
$attribute = new GroupExternalAttribute($group, $service, $key, $value);
123+
$this->groupExternalAttributes->persist($attribute);
124+
} catch (UniqueConstraintViolationException) {
125+
throw new BadRequestException("Attribute already exists.");
126+
}
120127

121128
$this->sendSuccessResponse("OK");
122129
}
@@ -135,7 +142,7 @@ public function checkRemove()
135142
#[Query("service", new VString(1, 32), "Identifier of the external service creating the attribute", required: true)]
136143
#[Query("key", new VString(1, 32), "Key of the attribute (must be valid identifier)", required: true)]
137144
#[Query("value", new VString(0, 255), "Value of the attribute (arbitrary string)", required: true)]
138-
#[Path("groupId", new VString(), required: true)]
145+
#[Path("groupId", new VUuid(), required: true)]
139146
public function actionRemove(string $groupId, string $service, string $key, string $value)
140147
{
141148
$attributes = $this->groupExternalAttributes->findBy(

0 commit comments

Comments
 (0)