<?php
declare(strict_types=1);
/*
* This file is part of the SynergyBot project.
*
* For the full copyright and license information,
* please read the LICENSE.md file that was distributed with this source code.
*
* The SymfonyBot project - inspiring people to chat!
*
* Copyright (c) 2022.
*/
namespace App\AjaxBundle\EventSubscriber;
use App\AjaxBundle\AjaxResponse;
use App\AjaxBundle\Exception\AjaxException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class AjaxExceptionSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
'kernel.exception' => 'onKernelException',
];
}
public function onKernelException(ExceptionEvent $event): void
{
/* @var AjaxException $exception */
$exception = $event->getThrowable();
if (!$exception instanceof AjaxException) {
return;
}
$response = new AjaxResponse($exception->getDetails(), $exception->getMessage(), Response::HTTP_UNPROCESSABLE_ENTITY);
$response->headers->set('Content-Type', 'application/problem+json');
$event->setResponse($response);
}
}