for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Nelexa\RequestDtoBundle\EventListener;
use Nelexa\RequestDtoBundle\Exception\RequestDtoValidationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Serializer\SerializerInterface;
final class RequestDtoExceptionListener
{
private SerializerInterface $serializer;
/**
* ExceptionListener constructor.
*/
public function __construct(SerializerInterface $serializer)
$this->serializer = $serializer;
}
public function onKernelException(ExceptionEvent $event): void
$exception = $event->getThrowable();
if ($exception instanceof RequestDtoValidationException) {
$format = $event->getRequest()->getContentType() ?? 'json';
if (!\in_array($format, ['json', 'xml'], true)) {
$format = 'json';
$responseObject = (string) $this->serializer->serialize($exception, $format);
$event->setResponse(
new Response(
$responseObject,
$exception->getStatusCode(),
[...$exception->getHeaders(), 'Content-Type' => 'application/problem+' . $format]
)
);