Symfony/Event/KernelExceptionListener.php line 24

Open in your IDE?
  1. <?php 
  2. namespace App\Event;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use App\Slack\ChannelNotifier;
  8. class KernelExceptionListener implements EventSubscriberInterface
  9. {    
  10.     protected $environment;
  11.     
  12.     protected $slack;
  13.     
  14.     public function __construct$environmentChannelNotifier $slack )
  15.     {
  16.         $this->environment $environment;
  17.         $this->slack $slack;
  18.     }
  19.     
  20.     public function onKernelException(ExceptionEvent $event)
  21.     {   
  22.         if( $this->environment != 'prod' )
  23.             return;
  24.                 
  25.         $exception $event->getThrowable();
  26.         
  27.         if( $exception instanceof HttpExceptionInterface ){
  28.         
  29.             $statusCode $exception->getStatusCode();
  30.             $request $event->getRequest();            
  31.         
  32.         } else {
  33.         
  34.             $statusCode 500;
  35.             $request false;
  36.         
  37.         }
  38.         
  39.         if($statusCode 500// we're not interested in 404s
  40.             return;
  41.         
  42.         $message sprintf('ERROR `%d` (%s) at %s: "%s" in `%s` on `line %s`'
  43.             $statusCode
  44.             get_class($exception),
  45.             $request $request->getUri() : $_SERVER["REQUEST_URI"], 
  46.             $exception->getMessage(),
  47.             $exception->getFile(),
  48.             $exception->getLine()
  49.         ); 
  50.         
  51.         $this->slack->send($message);
  52.     }    
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return array(
  56.             KernelEvents::EXCEPTION => array('onKernelException', -128),
  57.         );
  58.     }
  59. }