Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.43% |
27 / 28 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RenderErrorListener | |
96.43% |
27 / 28 |
|
66.67% |
2 / 3 |
14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| onKernelException | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\EventListeners; |
| 3 | |
| 4 | use Apie\Common\ErrorHandler\ApiErrorRenderer; |
| 5 | use Apie\Common\IntegrationTestLogger; |
| 6 | use Apie\Core\ContextConstants; |
| 7 | use Apie\Core\Exceptions\HttpStatusCodeException; |
| 8 | use Apie\HtmlBuilders\ErrorHandler\CmsErrorRenderer; |
| 9 | use Psr\Log\LoggerInterface; |
| 10 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 11 | use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 12 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 13 | use Symfony\Component\HttpKernel\KernelEvents; |
| 14 | |
| 15 | class RenderErrorListener implements EventSubscriberInterface |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly ?CmsErrorRenderer $cmsErrorRenderer, |
| 19 | private readonly ApiErrorRenderer $apiErrorRenderer, |
| 20 | private readonly LoggerInterface $logger, |
| 21 | private readonly string $cmsBaseUrl |
| 22 | ) { |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return array<string, array<int, int|string>> |
| 27 | */ |
| 28 | public static function getSubscribedEvents(): array |
| 29 | { |
| 30 | return [ |
| 31 | KernelEvents::EXCEPTION => ['onKernelException', -1], |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | public function onKernelException(ExceptionEvent $event): void |
| 36 | { |
| 37 | $this->logger->error( |
| 38 | 'An error occured in Symfony: ' . $event->getThrowable()->getMessage(), |
| 39 | ['error' => $event->getThrowable()] |
| 40 | ); |
| 41 | if (!$event->isMainRequest()) { |
| 42 | return; |
| 43 | } |
| 44 | $request = $event->getRequest(); |
| 45 | if ($request->attributes->has('_is_apie')) { |
| 46 | if ($request->attributes->has(ContextConstants::CMS) |
| 47 | && null !== $this->cmsErrorRenderer |
| 48 | && !$event->hasResponse() |
| 49 | ) { |
| 50 | $event->setResponse($this->cmsErrorRenderer->createCmsResponse($event->getRequest(), $event->getThrowable())); |
| 51 | } else { |
| 52 | $event->setResponse($this->apiErrorRenderer->createApiResponse($event->getThrowable())); |
| 53 | } |
| 54 | } |
| 55 | $exception = $event->getThrowable(); |
| 56 | IntegrationTestLogger::logException($exception); |
| 57 | if ($exception instanceof NotFoundHttpException) { |
| 58 | if (str_starts_with($request->getPathInfo(), $this->cmsBaseUrl) |
| 59 | && null !== $this->cmsErrorRenderer |
| 60 | && !$event->hasResponse() |
| 61 | ) { |
| 62 | $event->setResponse($this->cmsErrorRenderer->createCmsResponse($event->getRequest(), $event->getThrowable())); |
| 63 | } |
| 64 | } |
| 65 | if ($exception instanceof HttpStatusCodeException) { |
| 66 | $response = $event->getResponse(); |
| 67 | if ($response) { |
| 68 | $response->setStatusCode($exception->getStatusCode()); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |