Skip to content

Commit 782eab2

Browse files
committed
4992: Cleaned up how exceptions are mapped to http exceptions
1 parent bab679a commit 782eab2

File tree

12 files changed

+199
-139
lines changed

12 files changed

+199
-139
lines changed

config/packages/api_platform.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,22 @@ api_platform:
5050
5151
license:
5252
name: MIT
53+
54+
# @see https://api-platform.com/docs/core/errors/#exception-to-status-configuration-using-symfony
55+
exception_to_status:
56+
# The 4 following handlers are registered by default, keep those lines to prevent unexpected side effects
57+
Symfony\Component\Serializer\Exception\ExceptionInterface: 400 # Use a raw status code (recommended)
58+
ApiPlatform\Exception\InvalidArgumentException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
59+
ApiPlatform\ParameterValidator\Exception\ValidationExceptionInterface: 400
60+
Doctrine\ORM\OptimisticLockException: 409
61+
62+
# Validation exception
63+
ApiPlatform\Validator\Exception\ValidationException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY
64+
65+
# App exception mappings
66+
App\Exceptions\BadRequestException: 400
67+
App\Exceptions\ForbiddenException: 403
68+
App\Exceptions\NotFoundException: 404
69+
App\Exceptions\NotAcceptableException: 406
70+
App\Exceptions\ConflictException: 409
71+
App\Exceptions\TooManyRequestsException: 429

public/fixture/resources.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"ResourceMail": "[email protected]",
4+
"allowInstantBooking": "True"
5+
}
6+
]

src/Controller/InteractiveController.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
use App\Entity\ScreenUser;
88
use App\Entity\Tenant\Slide;
9-
use App\Entity\User;
10-
use App\Exceptions\InteractiveSlideException;
9+
use App\Exceptions\BadRequestException;
10+
use App\Exceptions\ConflictException;
11+
use App\Exceptions\NotAcceptableException;
12+
use App\Exceptions\TooManyRequestsException;
1113
use App\Service\InteractiveSlideService;
1214
use Symfony\Bundle\SecurityBundle\Security;
1315
use Symfony\Component\HttpFoundation\JsonResponse;
1416
use Symfony\Component\HttpFoundation\Request;
1517
use Symfony\Component\HttpKernel\Attribute\AsController;
16-
use Symfony\Component\HttpKernel\Exception\HttpException;
17-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1819

1920
#[AsController]
2021
final readonly class InteractiveController
@@ -24,27 +25,27 @@ public function __construct(
2425
private Security $security,
2526
) {}
2627

28+
/**
29+
* @throws ConflictException
30+
* @throws BadRequestException
31+
* @throws NotAcceptableException
32+
* @throws TooManyRequestsException
33+
*/
2734
public function __invoke(Request $request, Slide $slide): JsonResponse
2835
{
29-
$requestBody = $request->toArray();
36+
$user = $this->security->getUser();
3037

31-
try {
32-
$interactionRequest = $this->interactiveSlideService->parseRequestBody($requestBody);
33-
} catch (InteractiveSlideException $e) {
34-
throw new HttpException($e->getCode(), $e->getMessage());
38+
if (!($user instanceof ScreenUser)) {
39+
throw new AccessDeniedHttpException('Only screen user can perform action.');
3540
}
3641

37-
$user = $this->security->getUser();
42+
$tenant = $user->getActiveTenant();
3843

39-
if (!($user instanceof User || $user instanceof ScreenUser)) {
40-
throw new NotFoundHttpException('User not found');
41-
}
44+
$requestBody = $request->toArray();
4245

43-
try {
44-
$actionResult = $this->interactiveSlideService->performAction($user, $slide, $interactionRequest);
45-
} catch (InteractiveSlideException $e) {
46-
throw new HttpException($e->getCode(), $e->getMessage());
47-
}
46+
$interactionRequest = $this->interactiveSlideService->parseRequestBody($requestBody);
47+
48+
$actionResult = $this->interactiveSlideService->performAction($tenant, $slide, $interactionRequest);
4849

4950
return new JsonResponse($actionResult);
5051
}

src/Entity/Tenant/InteractiveSlide.php renamed to src/Entity/Tenant/InteractiveSlideConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use Symfony\Component\Serializer\Annotation\Ignore;
1010

1111
#[ORM\Entity(repositoryClass: InteractiveSlideRepository::class)]
12-
class InteractiveSlide extends AbstractTenantScopedEntity
12+
#[ORM\Table(name: "interactive_slide")]
13+
class InteractiveSlideConfig extends AbstractTenantScopedEntity
1314
{
1415
#[Ignore]
1516
#[ORM\Column(nullable: true)]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
class ForbiddenException extends \Exception
6+
{
7+
8+
}

src/Exceptions/InteractiveSlideException.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
class NotAcceptableException extends \Exception
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
class TooManyRequestsException extends \Exception
6+
{
7+
8+
}

0 commit comments

Comments
 (0)