Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format error details & violations #146

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Api/Controller/MissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Api\Controller;

use App\Api\Exception\KillerBadRequestHttpException;
use App\Api\Exception\KillerValidationException;
use App\Domain\KillerSerializerInterface;
use App\Domain\KillerValidatorInterface;
use App\Domain\Mission\Entity\Mission;
Expand Down Expand Up @@ -46,7 +45,7 @@ public function createMission(
$room = $player->getRoom();

if (!$room || $room->getStatus() !== Room::PENDING) {
throw new KillerBadRequestHttpException('KILLER_CAN_NOT_ADD_MISSIONS');
throw new KillerBadRequestHttpException('CAN_NOT_ADD_MISSIONS');
}

$player->addAuthoredMission($mission);
Expand Down Expand Up @@ -82,11 +81,7 @@ public function patchMission(Request $request, Mission $mission): JsonResponse
],
);

try {
$this->validator->validate($mission);
} catch (KillerValidationException $e) {
throw new KillerBadRequestHttpException($e->getMessage());
}
$this->validator->validate($mission);

$this->persistenceAdapter->flush();

Expand Down
8 changes: 1 addition & 7 deletions src/Api/Controller/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace App\Api\Controller;

use App\Api\Exception\KillerBadRequestHttpException;
use App\Api\Exception\KillerValidationException;
use App\Application\UseCase\Player\ChangeRoomUseCase;
use App\Domain\KillerSerializerInterface;
use App\Domain\KillerValidatorInterface;
Expand Down Expand Up @@ -155,11 +153,7 @@ public function patchPlayer(Request $request, Player $player): JsonResponse
],
);

try {
$this->validator->validate($player);
} catch (KillerValidationException $e) {
throw new KillerBadRequestHttpException($e->getMessage());
}
$this->validator->validate($player);

$this->logger->info('KILLER : player target : {player_target}', ['player_target' => $player->getTarget()]);

Expand Down
9 changes: 2 additions & 7 deletions src/Api/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Api\Controller;

use App\Api\Exception\KillerBadRequestHttpException;
use App\Api\Exception\KillerValidationException;
use App\Application\UseCase\Player\ChangeRoomUseCase;
use App\Domain\KillerSerializerInterface;
use App\Domain\KillerValidatorInterface;
Expand Down Expand Up @@ -89,7 +88,7 @@ public function patchRoom(Request $request, Room $room): JsonResponse

if (!$transitionSuccess) {
// CAN_NOT_MOVE_TO_IN_GAME or CAN_NO_MOVE_TO_ENDED
throw new KillerBadRequestHttpException(sprintf('KILLER_CAN_NOT_MOVE_TO_%s', $data['status']));
throw new KillerBadRequestHttpException(sprintf('CAN_NOT_MOVE_TO_%s', $data['status']));
}
} catch (\DomainException $e) {
throw new BadRequestHttpException($e->getMessage());
Expand All @@ -106,11 +105,7 @@ public function patchRoom(Request $request, Room $room): JsonResponse
],
);

try {
$this->validator->validate($room);
} catch (KillerValidationException $e) {
throw new KillerBadRequestHttpException($e->getMessage());
}
$this->validator->validate($room);

$this->persistenceAdapter->flush();

Expand Down
4 changes: 3 additions & 1 deletion src/Api/Exception/KillerBadRequestHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace App\Api\Exception;

class KillerBadRequestHttpException extends KillerHttpException
use App\Domain\KillerExceptionInterface;

class KillerBadRequestHttpException extends KillerHttpException implements KillerExceptionInterface
{
public function __construct(string $message = '', ?\Throwable $previous = null, int $code = 0, array $headers = [])
{
Expand Down
3 changes: 2 additions & 1 deletion src/Api/Exception/KillerHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace App\Api\Exception;

use App\Domain\KillerExceptionInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;

class KillerHttpException extends HttpException
class KillerHttpException extends HttpException implements KillerExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Application/UseCase/Player/ChangeRoomUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(private EventDispatcherInterface $eventDispatcher)
public function execute(Player $player, ?Room $newRoom = null): void
{
if ($newRoom?->getStatus() === Room::IN_GAME) {
throw new PlayerCanNotJoinRoomException('KILLER_ROOM_ALREADY_IN_GAME');
throw new PlayerCanNotJoinRoomException('ROOM_ALREADY_IN_GAME');
}

$previousRoom = $player->getRoom();
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/KillerExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Domain;

interface KillerExceptionInterface
{
public function getMessage(): string;
}
4 changes: 2 additions & 2 deletions src/Domain/Mission/Entity/Mission.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Mission
#[Assert\Length(
min: 5,
max: 1000,
minMessage: 'KILLER_MISSION_TOO_SHORT_CONTENT',
maxMessage: 'KILLER_MISSION_TOO_LONG_CONTENT',
minMessage: 'MISSION_TOO_SHORT_CONTENT',
maxMessage: 'MISSION_TOO_LONG_CONTENT',
)]
#[Groups(['get-mission', 'get-player', 'post-mission', 'me'])]
private string $content;
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Player/Entity/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Player implements UserInterface
#[Assert\Length(
min: 2,
max: 30,
minMessage: 'KILLER_PLAYER_NAME_TOO_SHORT_CONTENT',
maxMessage: 'KILLER_PLAYER_NAME_TOO_LONG_CONTENT',
minMessage: 'PLAYER_NAME_TOO_SHORT_CONTENT',
maxMessage: 'PLAYER_NAME_TOO_LONG_CONTENT',
)]
private string $name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Player\Exception;

class PlayerCanNotJoinRoomException extends \DomainException
use App\Domain\KillerExceptionInterface;

class PlayerCanNotJoinRoomException extends \DomainException implements KillerExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Player\Exception;

class PlayerHasNoKillerOrTargetException extends \DomainException
use App\Domain\KillerExceptionInterface;

class PlayerHasNoKillerOrTargetException extends \DomainException implements KillerExceptionInterface
{
}
4 changes: 3 additions & 1 deletion src/Domain/Player/Exception/PlayerNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Player\Exception;

class PlayerNotFoundException extends \DomainException
use App\Domain\KillerExceptionInterface;

class PlayerNotFoundException extends \DomainException implements KillerExceptionInterface
{
}
4 changes: 3 additions & 1 deletion src/Domain/Player/Exception/PlayerNotInRoomException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Player\Exception;

class PlayerNotInRoomException extends \DomainException
use App\Domain\KillerExceptionInterface;

class PlayerNotInRoomException extends \DomainException implements KillerExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Domain/Room/Entity/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Room

#[ORM\Column(type: 'string', length: 255)]
#[Groups(['get-room', 'me', 'patch-room', 'publish-mercure'])]
#[Assert\Length(min: 2, max: 50, minMessage: 'KILLER_TOO_SHORT_CONTENT', maxMessage: 'KILLER_TOO_LONG_CONTENT')]
#[Assert\Length(min: 2, max: 50, minMessage: 'TOO_SHORT_CONTENT', maxMessage: 'TOO_LONG_CONTENT')]
private string $name;

#[ORM\Column(type: 'string', length: 255, options: ['default' => self::PENDING])]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Room\Exception;

class CanNotApplyRoomTransitionException extends \DomainException
use App\Domain\KillerExceptionInterface;

class CanNotApplyRoomTransitionException extends \DomainException implements KillerExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Room\Exception;

class NotEnoughMissionsInRoomException extends \DomainException
use App\Domain\KillerExceptionInterface;

class NotEnoughMissionsInRoomException extends \DomainException implements KillerExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Room\Exception;

class NotEnoughPlayersInRoomException extends \DomainException
use App\Domain\KillerExceptionInterface;

class NotEnoughPlayersInRoomException extends \DomainException implements KillerExceptionInterface
{
}
4 changes: 3 additions & 1 deletion src/Domain/Room/Exception/RoomNotInGameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Domain\Room\Exception;

class RoomNotInGameException extends \DomainException
use App\Domain\KillerExceptionInterface;

class RoomNotInGameException extends \DomainException implements KillerExceptionInterface
{
}
26 changes: 14 additions & 12 deletions src/Infrastructure/Serializer/KillerProblemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

namespace App\Infrastructure\Serializer;

use App\Domain\KillerExceptionInterface;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;

#[AsDecorator(decorates: ProblemNormalizer::class)]
readonly class KillerProblemNormalizer implements NormalizerInterface
class KillerProblemNormalizer implements NormalizerInterface, SerializerAwareInterface
{
use SerializerAwareTrait;

public const CUSTOM_ERROR_PREFIX = 'KILLER';

public function __construct(#[AutowireDecorated] private ProblemNormalizer $inner)
Expand All @@ -25,22 +29,20 @@ public function normalize(
?string $format = null,
array $context = [],
): float|int|bool|\ArrayObject|array|string|null {
$this->inner->setSerializer($this->serializer);
$normalizedException = $this->inner->normalize($object, $format, $context);

if ($context['exception'] instanceof HttpExceptionInterface) {
$errorDetail = $context['exception']->getMessage();
}

if ($context['exception'] instanceof ValidationFailedException) {
$errorDetail = $context['exception']->getValue();
if ($context['exception'] instanceof KillerExceptionInterface) {
$normalizedException['detail'] = $context['exception']->getMessage();
}

if (isset($errorDetail) && is_string($errorDetail)) {
if ($context['exception'] instanceof HttpExceptionInterface) {
$errorDetail = $context['exception']->getMessage();
$errorDetail = explode('_', $errorDetail, 2);
}

if (isset($errorDetail[0]) && $errorDetail[0] === self::CUSTOM_ERROR_PREFIX) {
$normalizedException['detail'] = $errorDetail[1];
if (isset($errorDetail[0]) && $errorDetail[0] === self::CUSTOM_ERROR_PREFIX) {
$normalizedException['detail'] = $errorDetail[1];
}
}

return $normalizedException;
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Validator/KillerValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function validate(object $entity): void
return;
}

throw new KillerValidationException('KILLER_VALIDATION_ERROR', $violations);
throw new KillerValidationException('VALIDATION_ERROR', $violations);
}
}
Loading
Loading