Created
December 4, 2020 11:58
-
-
Save mdzwigala/9e9c89b1972ff1cbf3337a89211a4504 to your computer and use it in GitHub Desktop.
ADR-EXAMPLE\Infrastructure\DataValidationExceptionHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Infrastructure\ExceptionHandler; | |
use App\Infrastructure\Exception\DataValidationException; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | |
final class DataValidationExceptionHandler | |
{ | |
public function __invoke(ExceptionEvent $exceptionEvent): void | |
{ | |
$exception = $exceptionEvent->getThrowable(); | |
if (!$exception instanceof DataValidationException) { | |
return; | |
} | |
$exceptionEvent->setResponse( | |
new JsonResponse($exception->getErrors(), $exception->getStatusCode()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment