Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found something that works. The <a href="http://symfony.com/doc/current/book/internals.html#kernel-exception-event">Symfony2 internals doc on the kernel.exeption event</a> mention that a response can be set on the event, and the <a href="http://api.symfony.com/2.0/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.html">GetResponseForExceptionEvent docs</a> say</p> <blockquote> <p>The propagation of this event is stopped as soon as a response is set.</p> </blockquote> <p>I cobbled together a listener that appears to do just what I want:</p> <pre class="lang-php prettyprint-override"><code>&lt;?php namespace Acme\DemoBundle\Listener; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; class ExceptionLoggingListener { private $logger; public function __construct(LoggerInterface $logger) { $this-&gt;logger = $logger; } public function onKernelException(GetResponseForExceptionEvent $event) { if(!$event) { $this-&gt;logger-&gt;err("Unknown kernel.exception in ".__CLASS__); return; } $notFoundException = '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException'; $e = $event-&gt;getException(); $type = get_class($e); if ($e instanceof $notFoundException) { $this-&gt;logger-&gt;info($e-&gt;getMessage()); $response = new Response(Response::$statusTexts[404], 404); $event-&gt;setResponse($response); return; } $accessDeniedException = '\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'; if ($e instanceof $accessDeniedException) { $this-&gt;logger-&gt;info($e-&gt;getMessage()); $response = new Response(Response::$statusTexts[403], 403); $event-&gt;setResponse($response); return; } $this-&gt;logger-&gt;err("kernel.exception of type $type. Message: '".$e-&gt;getMessage()."'\nFile: ".$e-&gt;getFile().", line ".$e-&gt;getLine()."\nTrace: ".$e-&gt;getTraceAsString()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload