<?php
/**
* Created by PhpStorm.
* User: grigor
* Date: 9/25/18
* Time: 4:39 PM
*/
namespace App\EventSubscriber;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\KernelInterface;
class ExceptionSubscriber implements EventSubscriberInterface
{
/**
* @var KernelInterface
*/
private $kernel;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(KernelInterface $kernel, LoggerInterface $logger)
{
$this->kernel = $kernel;
$this->logger = $logger;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$e = $event->getException();
if ($e instanceof NotFoundHttpException) {
$response = new JsonResponse('', Response::HTTP_NOT_FOUND);
} else {
$responseData = ['message' => 'Unidentified internal error'];
$devMessage = [
'message' => $e->getMessage(),
'type' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine()
];
$responseData['dev_message'] = $devMessage;
$this->logger->error(print_r($responseData, true));
if ($this->kernel->getEnvironment() != 'dev') {
unset($responseData['dev_message']);
}
$response = new JsonResponse($responseData, Response::HTTP_CONFLICT);
}
$event->setResponse($response);
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::EXCEPTION => 'onKernelException'
);
}
}