vendor/ekino/newrelic-bundle/Listener/ExceptionListener.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Ekino New Relic bundle.
  5.  *
  6.  * (c) Ekino - Thomas Rabaix <thomas.rabaix@ekino.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Ekino\NewRelicBundle\Listener;
  12. use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  15. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  16. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. /**
  19.  * Listen to exceptions dispatched by Symfony to log them to NewRelic.
  20.  */
  21. class ExceptionListener implements EventSubscriberInterface
  22. {
  23.     private $interactor;
  24.     public function __construct(NewRelicInteractorInterface $interactor)
  25.     {
  26.         $this->interactor $interactor;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             KernelEvents::EXCEPTION => ['onKernelException'0],
  32.         ];
  33.     }
  34.     /**
  35.      * @param GetResponseForExceptionEvent|ExceptionEvent $event
  36.      */
  37.     public function onKernelException(KernelExceptionEvent $event): void
  38.     {
  39.         $exception method_exists($event'getThrowable') ? $event->getThrowable() : $event->getException();
  40.         if (!$exception instanceof HttpExceptionInterface) {
  41.             $this->interactor->noticeThrowable($exception);
  42.         }
  43.     }
  44. }
  45. if (!class_exists(KernelExceptionEvent::class)) {
  46.     if (class_exists(ExceptionEvent::class)) {
  47.         class_alias(ExceptionEvent::class, KernelExceptionEvent::class);
  48.     } else {
  49.         class_alias(GetResponseForExceptionEvent::class, KernelExceptionEvent::class);
  50.     }
  51. }