title: How to report error in production on Symfony2 with a custom ERROR 500 template and an EventListener?
body: <p>I want to report my error messages on a custom error 500 template. So, I've followed the <a href="http://symfony.com/doc/current/cookbook/controller/error_pages.html#overriding-the-default-error-templates" rel="nofollow">official symfony tutorial</a> on <em>overriding the default error templates</em>:</p>
  
  <pre><code>app/
  └─ Resources/
     └─ TwigBundle/
        └─ views/
           └─ Exception/
              ├─ error404.html.twig
              ├─ error403.html.twig
              ├─ error.html.twig      # All other HTML errors (including 500)
              ├─ error404.json.twig
              ├─ error403.json.twig
              └─ error.json.twig      # All other JSON errors (including 500)
  </code></pre>
  
  <p>And my <code>error500.html.twig</code> page template looks like that:</p>
  
  <pre><code>            &lt;div class=" details"&gt;
                &lt;h3&gt;Oops! Something went wrong.&lt;/h3&gt;
                &lt;p&gt; We are fixing it! Please come back in a while.
                    &lt;br/&gt; TEST&lt;/p&gt;
                    &lt;div class="alert alert-danger display-hide" {{message ? 'style="display: block;"'}}&gt;
                        &lt;button class="close" data-close="alert"&gt;&lt;/button&gt;
                        &lt;span&gt;{{message ? message | trans}}&lt;/span&gt;
                    &lt;/div&gt;
                  &lt;p&gt;
                      &lt;a href="{{path('homepage')}}" class="btn red btn-outline"&gt; OK, take me to the homepage &lt;/a&gt;
                      &lt;br&gt; &lt;/p&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>Now, I want to write a particular <code>message</code> parameter by using the ExceptionListener that listen <em>onKernelException</em>, as described on the <a href="http://symfony.com/doc/current/cookbook/event_dispatcher/event_listener.html#creating-an-event-listener" rel="nofollow">Symfony cookbook</a>:</p>
  
  <pre><code>use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  
  class ExceptionListener
  {
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
      // You get the exception object from the received event
      $exception = $event-&gt;getException();
      $message = sprintf(
          'My Error says: %s with code: %s',
          $exception-&gt;getMessage(),
          $exception-&gt;getCode()
      );
  
      // Customize your response object to display the exception details
      $response = new Response();
      $response-&gt;setContent($message);
  
      // HttpExceptionInterface is a special type of exception that
      // holds status code and header details
      if ($exception instanceof HttpExceptionInterface) {
          $response-&gt;setStatusCode($exception-&gt;getStatusCode());
          $response-&gt;headers-&gt;replace($exception-&gt;getHeaders());
      } else {
          $response-&gt;setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
      }
  
      // Send the modified response object to the event
      $event-&gt;setResponse($response);
    }
  }
  </code></pre>
  
  <p>I think that I'm only missing the proper way to set the correct template of the <code>Symfony\Component\HttpFoundation\Response</code>. Actually I've been able to intercept the kernel exception event but I return a classic HTML error page without any template.</p>
  
  <p>I'm using <strong>PHP 7</strong> and <strong>Symfony 2.8.4</strong>.</p>
  
  <h1>EDIT</h1>
  
  <p>I've found the proper way to intercept the type of exception through the STATUS CODE:</p>
  
  <pre><code>public function onKernelException(GetResponseForExceptionEvent $event)
   {
      $exception = $event-&gt;getException();
      $statusCode = $exception-&gt;getStatusCode();
      $message = sprintf(
          'ERROR: %s with code: %s',
          $exception-&gt;getMessage(),
          $exception-&gt;getCode()
      );
  
      switch ($statusCode) {
        case 404:
            $errorTemplate = 'TwigBundle:Exception:error404.html.twig';
            break;
        case 403:
            $errorTemplate = 'TwigBundle:Exception:error403.html.twig';
            break;
        default:
            $errorTemplate = 'TwigBundle:Exception:error500.html.twig';
            break;
      }
      $response = $this-&gt;templating-&gt;renderResponse(
          $errorTemplate,
          ['message' =&gt; $message]
          );
  
      // HttpExceptionInterface is a special type of exception that
      // holds status code and header details
      if ($exception instanceof HttpExceptionInterface) {
          $response-&gt;setStatusCode($exception-&gt;getStatusCode());
          $response-&gt;headers-&gt;replace($exception-&gt;getHeaders());
      } else {
          $response-&gt;setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
      }
  
      $event-&gt;setResponse($response);
  }
  </code></pre>
  
  <ul>
  <li><a href="http://symfony.com/doc/current/components/http_kernel/introduction.html#handling-exceptions-the-kernel-exception-event" rel="nofollow">Handling exception event in Symfony</a></li>
  <li><a href="http://api.symfony.com/3.0/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.html#method_getException" rel="nofollow">public Exception getException()</a></li>
  <li><a href="http://api.symfony.com/3.0/Symfony/Component/HttpKernel/Exception.html" rel="nofollow">Symfony\Component\HttpKernel\Exception</a></li>
  <li><a href="http://api.symfony.com/3.0/Symfony/Component/HttpKernel/Exception/NotFoundHttpException.html" rel="nofollow">GetStatusCode()</a></li>
  </ul>