Skip to content

Instantly share code, notes, and snippets.

@mdzwigala
Created December 4, 2020 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdzwigala/9e9c89b1972ff1cbf3337a89211a4504 to your computer and use it in GitHub Desktop.
Save mdzwigala/9e9c89b1972ff1cbf3337a89211a4504 to your computer and use it in GitHub Desktop.
ADR-EXAMPLE\Infrastructure\DataValidationExceptionHandler
<?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