title: Custom ExceptionListener picks up 403 & 404, but not 500 errors
body: <p>Exact same problem as unanswered question <a href="https://stackoverflow.com/questions/21139117/symfony-2-4-why-are-500-errors-not-caught-by-kernel-exception-listener">Symfony 2.4: Why are 500 errors not caught by kernel.exception listener</a>.</p>
  
  <p>I have implemented a custom exception "handler" which works awesomely for 403 and 404 issues, but 500 errors (which is what I REALLY want to handle, as I want to send an email to myself from the system when this happens) does not trigger my custom "handler" and continues to behave as if the custom "handler" was not there. The code is relatively straight forward:</p>
  
  <p>Extract from app/config/config.yml:</p>
  
  <pre><code>services:
      core.exceptlistener:
          class: Pmb\LicensingBundle\Listener\ExceptionListener
          arguments:  ["@service_container", "@router"]
          tags:
              - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 200 }
  </code></pre>
  
  <p>Entire \Pmb\LicensingBundle\Listener\ExceptionListener.php:</p>
  
  <pre><code>&lt;?php
  
  namespace Pmb\LicensingBundle\Listener;
  
  use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Symfony\Bundle\TwigBundle\TwigEngine;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  
  class ExceptionListener
  {
      private $container;
      private $router;
  
      function __construct($container, $router) 
      {
          $this-&gt;container = $container;
          $this-&gt;router = $router;
      }
  
      public function onKernelException(GetResponseForExceptionEvent $event)
      {
          $exception = $event-&gt;getException();
          die("test");
          $request = $this-&gt;container-&gt;get('request');
          $templating = $this-&gt;container-&gt;get('templating');    
  
          if ($exception instanceof \Symfony\Component\Security\Core\Exception\AccessDeniedException)
          {
  
              # If AJAX request, do show not error page.
              if ($request-&gt;isXmlHttpRequest()) 
              {
                  $response = new Response(json_encode(array('error' =&gt; 'Access Denied: Not logged in as administrator')));
              }
              else
              {
                  return new RedirectResponse($this-&gt;router-&gt;generate('login'));
              }
  
              $event-&gt;setResponse($response);
          }
          elseif ($exception-&gt;getStatusCode() == 404)
          {
              # If AJAX request, do show not error page.
              if ($request-&gt;isXmlHttpRequest()) 
              {
                  $response = new Response(json_encode(array('error' =&gt; 'Requested route could not be found')));
              }
              else
              {
                  $response = new Response($templating-&gt;render('PmbLicensingBundle:Exception:error404.html.twig', array(
                      'exception' =&gt; $exception
                  )));
              }
  
              $event-&gt;setResponse($response);
          }       
          else
          {
  
              # TODO: Send Email
              # If AJAX request, do not show error page.
              if ($request-&gt;isXmlHttpRequest()) $response = new Response(json_encode(array('error' =&gt; 'Internal Server Error Encountered. Developer has been notified.')));
              else
              {
                  $response = new Response($templating-&gt;render('PmbLicensingBundle:Exception:error500.html.twig', array(
                      'exception' =&gt; $exception
                  )));
              }
  
          }
      }
  }
  </code></pre>
  
  <p>I put the <code>die("test")</code> in there to verify that the "handler" is not being called at all and that is not just a problem with my if-else logic. As stated before, this works awesomely with any 404 or 403 errors, but 500 errors completely ignores this and behaves in the default manner. I am pretty sure that this has to do with the registering of the listener service, but I cannot find anything that explains how to make it work properly.</p>
  
  <p><strong>EDIT:</strong> Below are screenshots of errors that are not being handled as expected. I notice on the one (undefined variable) the <code>die("test");</code> actually displays at the bottom, but on the ohter (syntax error) it does not display, even though both seems like they are being caught by Symfony2. Further testing showed that <code>die("test");</code> showed up, but die(">> .$exception->getStatusCode()." &lt;&lt;"); did not. I am assuming that this is causing a second exception which I am not seeing, just like AccessDeniedException did not have this function call and I had to use <code>instanceof</code>, but there are many possible errors that can come up with a 500 error, so how do I distinguish whether the error is one of these?</p>
  
  <p><img src="https://i.stack.imgur.com/p8T1c.jpg" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/5E8DP.jpg" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/V2MeV.jpg" alt="enter image description here"></p>
  
  <p><strong>FURTHER EDIT:</strong> On the error that did print the "Test" at the bottom, I noticed that when I do <code>$container-&gt;testError();</code>, I do not get the "Test", at the bottom of the error, but when I do <code>return $container;</code>, I do, even though both errors are ContextErrorException with undefined variable.</p>