2

I have a problem with throwing Symfony 2 exception inside event listner. When I do it I get Uncaught exception (onRequest method of listener). Is there any way to throw exception in listener that can be caught by Symfony.

I was trying to avoid this problem by changing the controller in case of an exception (onController method of listener). Target controller had just one line:

throw $this->createNotFoundException('Test');

The controller swap worked but it also resulted in uncaught exception error.

How to wrap a listener or swapped controller inside symfony exception catcher.

My listener:

namespace AppBundle\Listener;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use AppBundle\Controller\HomeController;

class SessionListener {

    private $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    public function onController(FilterControllerEvent $event)
    {
        $controller=new HomeController();

        $replacementController= array($controller, 'notFoundAction');

        $event->setController($replacementController);
    }

    public function onRequest(Event $event) {
        $session = $this->container->get('session'); 
        $kernel = $this->container->get('kernel');

        throw new NotFoundHttpException('Test');
    }

}

My services.yml

services:
    app.session_listener:
        class: AppBundle\Listener\SessionListener
        arguments: [@service_container]    
        tags:
         - { name: kernel.event_listener, event: kernel.request, method: onRequest }
         - { name: kernel.event_listener, event: kernel.controller, method: onController } 

2
  • 1
    It might be that symfony calls a subrequest on exception (see Symfony\Component\HttpKernel\EventListener\EventListener:50) and if you're throwing an exception in the onRequest method this exception won't be handled properly by the default exception controller (Symfony\Bundle\TwigBundle\Controller\ExceptionController:showAction). If you want full control on exception handling I'd look into implementing your custom exception listener. If you just want to return something directly from onRequest you can use $event->setResponse() to stop propagation and return your response. Commented Nov 25, 2015 at 22:18
  • I have tried this before and it works but I was hoping to catch this exception with default Symfony exception handler. Also it would be nice to redirect controler that can use application container and all its features. The setResponse method is good enough in my case. Commented Nov 30, 2015 at 11:14

2 Answers 2

1

probably with a try catch

public function onRequest(Event $event) {

 try{

  $session = $this->container->get('session'); 
  $kernel = $this->container->get('kernel');

 }catch(NotFoundHttpException ex){

  throw new NotFoundHttpException('Test');

 }




}
Sign up to request clarification or add additional context in comments.

1 Comment

This is wrong, if there is no exception catching in onRequest method this will also result in uncought exception error. I am looking for a way to catch this exception by symfony default exception handler. @mickadoo comment is better but not quite there.
0

As an alternative you can return a 404 response instead of throwing an exception:

class SessionListener
{
    public function onRequest(GetResponseEvent $event)
    {
        $event->setResponse(new Response('Test', 404));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.