src/Listener/ExceptionListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  6. use Symfony\Component\Translation\TranslatorInterface;
  7. use App\Exception\ExceptionWithTemplateHtml;
  8. use App\Exception\EntityException;
  9. use App\Exception\MailerException;
  10. class ExceptionListener
  11. {
  12.     protected $translator;
  13.     protected $twig;
  14.     public function __construct(TranslatorInterface $translator$twig)
  15.     {
  16.         $this->translator $translator;
  17.         $this->twig       $twig;
  18.     }
  19.     public function onKernelException(GetResponseForExceptionEvent $event)
  20.     {
  21.         // You get the exception object from the received event
  22.         $exception $event->getException();
  23.         //only for exception from listener
  24.         if($exception->getMessage() == $this->translator->trans('listener.accesstoken.confirmation')) {
  25.           // $message = sprintf(
  26.           //     'My Error says: %s with code: %s',
  27.           //     $exception->getMessage(),
  28.           //     $exception->getCode()
  29.           // );
  30.           // Customize your response object to display the exception details
  31.           $response = new Response();
  32.           $response->setContent(json_encode(array('error' => 'invalid_confirmation''error_description' => $exception->getMessage())));
  33.           // HttpExceptionInterface is a special type of exception that
  34.           // holds status code and header details
  35.           if ($exception instanceof HttpExceptionInterface) {
  36.               $response->setStatusCode($exception->getStatusCode());
  37.               $response->headers->replace($exception->getHeaders());
  38.           } else {
  39.               $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  40.           }
  41.           // Send the modified response object to the event
  42.           $event->setResponse($response);
  43.         }
  44.         // Depuis la désactivation du mode debug dans le seconde param du AppKernel, la gestion des erreurs n'est plus la même. Il récupère l'exception pour la formatter
  45.         else {
  46.           $exceptionCode = !method_exists($exception'getStatusCode') ? 400 $exception->getStatusCode();
  47.           $content = array(
  48.             'code'     => $exceptionCode,
  49.             'message'  => $exception->getMessage(),
  50.           );
  51.           // Si c'est un excption html, doit former le template html
  52.           if ($exception instanceof ExceptionWithTemplateHtml) {
  53.             $content['template'] = $this->twig->render($exception->template, ['data' => $exception->data]);
  54.             $content['reasons']  = $exception->data['reasons'];
  55.           }
  56.           if (($exception instanceof EntityException || $exception instanceof MailerException) && isset($exception->codeError)) {
  57.             $content['message'] = $this->translator->trans($exception->codeError);
  58.           }
  59.           $response = new Response();
  60.           $response->setContent(json_encode($content));
  61.           $response->setStatusCode($exceptionCode);
  62.           $exceptionCode method_exists($exception'getHeaders') ? $response->headers->replace($exception->getHeaders()) : null;
  63.           $event->setResponse($response);
  64.         }
  65.         return;
  66.     }
  67. }