Skip to content

Commit da0e98e

Browse files
authored
Merge pull request #260 from os2display/feature/4992-instant-book-exception-handling
Changed how exceptions are handled in InstantBook
2 parents a800c77 + d07526c commit da0e98e

13 files changed

+293
-159
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
- [#260](https://github.com/os2display/display-api-service/pull/260)
8+
- Changed how exceptions are handled in InstantBook.
9+
710
## [2.5.1] - 2025-06-23
811

912
- [#245](https://github.com/os2display/display-api-service/pull/245)

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

migrations/Version20250828084617.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
final class Version20250828084617 extends AbstractMigration
11+
{
12+
public function getDescription(): string
13+
{
14+
return 'Rename interactive_slide to interactive_slide_config';
15+
}
16+
17+
public function up(Schema $schema): void
18+
{
19+
$this->addSql('RENAME TABLE interactive_slide TO interactive_slide_config;');
20+
$this->addSql('ALTER TABLE interactive_slide_config RENAME INDEX idx_138e544d9033212a TO IDX_D30060259033212A');
21+
}
22+
23+
public function down(Schema $schema): void
24+
{
25+
$this->addSql('ALTER TABLE interactive_slide_config RENAME INDEX idx_d30060259033212a TO IDX_138E544D9033212A');
26+
$this->addSql('RENAME TABLE interactive_slide_config TO interactive_slide;');
27+
}
28+
}

src/Controller/InteractiveController.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
use App\Entity\ScreenUser;
88
use App\Entity\Tenant\Slide;
9-
use App\Entity\User;
10-
use App\Exceptions\NotFoundException;
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;
18+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1619

1720
#[AsController]
1821
final readonly class InteractiveController
@@ -22,18 +25,28 @@ public function __construct(
2225
private Security $security,
2326
) {}
2427

28+
/**
29+
* @throws ConflictException
30+
* @throws BadRequestException
31+
* @throws NotAcceptableException
32+
* @throws TooManyRequestsException
33+
*/
2534
public function __invoke(Request $request, Slide $slide): JsonResponse
2635
{
36+
$user = $this->security->getUser();
37+
38+
if (!($user instanceof ScreenUser)) {
39+
throw new AccessDeniedHttpException('Only screen user can perform action.');
40+
}
41+
42+
$tenant = $user->getActiveTenant();
43+
2744
$requestBody = $request->toArray();
2845

2946
$interactionRequest = $this->interactiveSlideService->parseRequestBody($requestBody);
3047

31-
$user = $this->security->getUser();
32-
33-
if (!($user instanceof User || $user instanceof ScreenUser)) {
34-
throw new NotFoundException('User not found');
35-
}
48+
$actionResult = $this->interactiveSlideService->performAction($tenant, $slide, $interactionRequest);
3649

37-
return new JsonResponse($this->interactiveSlideService->performAction($user, $slide, $interactionRequest));
50+
return new JsonResponse($actionResult);
3851
}
3952
}

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

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

1111
#[ORM\Entity(repositoryClass: InteractiveSlideRepository::class)]
12-
class InteractiveSlide extends AbstractTenantScopedEntity
12+
class InteractiveSlideConfig extends AbstractTenantScopedEntity
1313
{
1414
#[Ignore]
1515
#[ORM\Column(nullable: true)]

src/Exceptions/InteractiveSlideException.php renamed to src/Exceptions/ForbiddenException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace App\Exceptions;
66

7-
class InteractiveSlideException extends \Exception
7+
class ForbiddenException extends \Exception
88
{
99
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Exceptions;
6+
7+
class NotAcceptableException extends \Exception
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Exceptions;
6+
7+
class TooManyRequestsException extends \Exception
8+
{
9+
}

0 commit comments

Comments
 (0)