2

I am running Symfony 2.7.6 dev and I have a listener set up that is supposed to trigger on console.exception, but it does not trigger, it only displays the exception in the console as usual. For testing purposes, I have incorporated a console.terminate listener, which works fine. (I have also tested console.command and that also works fine).

For the life of me I cannot figure out why the console.exception event does not fire or why the console.exception listener does not trigger.

ConsoleExceptionListener Setup in config.yml

kernel.listener.command_dispatch:
        class: CompanyHidden\PortalBundle\Listener\ConsoleExceptionListener
        arguments:  ["@service_container", "@router"]
        tags:
            - { name: kernel.event_listener, event: console.exception, method: onConsoleException }
            - { name: kernel.event_listener, event: console.terminate, method: onConsoleTerminate }

ConsoleExceptionListener.php

<?php

namespace CompanyHidden\PortalBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ConsoleExceptionListener
{
    private $container;
    private $router;

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

    public function onConsoleTerminate()
    {
        die(">>>> TERMINATE TEST >>>>");
    }

    public function onConsoleException(ConsoleExceptionEvent $event)
    {
        die(">>> EXCEPTION TEST<<<<");
    }
}

Console Command

<?php
namespace CompanyHidden\PortalBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use CompanyHidden\PortalBundle\Classes\ImapMailbox;

class getEmailsCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('getEmails');   
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $context = $this->getContainer()->get('router')->getContext();
        $context->setScheme('http');

        # SIMULATED TERMINATION TEST
        //print ">>>> TERMINATED <<<<<\n";
        //return null;

        # SIMULATED EXCEPTION TEST
        $null = null;
        $null->getNull();

        //..... Rest of Code, not relevant
    }
}
?>

Console Exception Thrown (instead of triggering exception listener) Console Exception

2
  • I do not understand your question. What makes you think a fatal error triggers the exception listener? Commented Jan 5, 2016 at 11:23
  • @xabbuh Fatal error triggers the normal kernel.exception ExceptionListener under normal "in-app" operation. Why would fatal error NOT trigger an console.exception during command execution? It even shows on the second last line of the screenshot that it is an exception. FatalErrorException, but an exception nonetheless. Commented Jan 5, 2016 at 14:20

4 Answers 4

1

try to build your console listener by this way:

namespace CompanyHidden\PortalBundle\Listener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\ConsoleEvents;

class ConsoleExceptionListener implements EventSubscriberInterface
{
    /.../

    public static function getSubscribedEvents()
    {
        return [
            ConsoleEvents::EXCEPTION => 'onConsoleException',
            ConsoleEvents::TERMINATE => 'onConsoleTerminate'
        ];
    }

    public function onConsoleTerminate(ConsoleTerminateEvent $event)
    {
         die(">>>> TERMINATE TEST >>>>");
    }

    public function onConsoleException(ConsoleExceptionEvent $event)
    {
         die(">>> EXCEPTION TEST<<<<");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It does not seem to have made a difference. Do I need to update my listener service in my config.yml?
Nope, your config.yml seems good... I try the code above and it works.
Try to test your command without exception in order to check if the console.terminate listener works.
I tested the console.terminate and it works exactly as before, no problem. The console exception also works exactly as before, which is to say that onConsoleException() does not execute. I do not understand how your code works if the listener service stays as is, as there is no indication that getSubscribedEvents() is ever triggered. My main problem is that it seems that console.exception is never fired, I don't know why and I don't know how to check this to verify. Also, note that I have a normal ExceptionListener for "in-app" exceptions, and this "EventSubscriber" must not override that.
1

In the console file, boot the kernel with $kernel->boot();

$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$kernel->boot();

$kernel->getContainer()->get('event_dispatcher')->addListener(\Symfony\Component\Console\ConsoleEvents::COMMAND,
    function (\Symfony\Component\Console\Event\ConsoleCommandEvent $event) {
        var_dump("COMMAND EVENT");
    });

$kernel->getContainer()->get('event_dispatcher')->addListener(\Symfony\Component\Console\ConsoleEvents::EXCEPTION,
    function (\Symfony\Component\Console\Event\ConsoleExceptionEvent $event) {
        var_dump("EXCEPTION EVENT");
    });

$kernel->getContainer()->get('event_dispatcher')->addListener(\Symfony\Component\Console\ConsoleEvents::TERMINATE,
    function (\Symfony\Component\Console\Event\ConsoleTerminateEvent $event) {
        var_dump("TERMINATE EVENT");
    });


$application->run($input);

1 Comment

Thank you, I will look into this in the next week or so and accept your answer if it works.
1

Scoolnico's solution works perfectly with symfony 3.3. For information, 'console.exception' event has been replaced by 'console.error' in symfony 3.3 (https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions)

Comments

0

The config and listener look correct.

Check your app/console file to make sure the dispatcher is set.

It should look like this

$dispatcher = new EventDispatcher();

$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->setDispatcher($dispatcher);  // <-- Needed for events to work
$application->run($input);

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.