Search code examples
phpsymfonycommandwarningsnotice

How to prevent Symfony from quitting the whole program when getting a warning or notice


In Symfony, when we run a command, if there is any notice or warning, Symfony shows a big red message and quits the whole program.

I want an error to be emitted to the log, but I don't want the whole program to quit. Can anyone please tell me how to achieve this. thanks.

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

class NotQuitWithWarningOrNoticeCommand extends ContainerAwareCommand
{

protected function configure()
{
    $this
        ->setName('try:nored')
        ->setDescription('Avoid to show big red message')
        ->addArgument('type');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $type = $input->getArgument('type');
    if ($type == 'warning' || !$type) {
        trigger_error("a warning", E_USER_WARNING);
    }
    if ($type == 'notice' || !$type) {
        trigger_error("a notice", E_USER_NOTICE);
    }
    echo "Hi there!\n";
}

} 

(The code here is just to reproduce the problem. Getting a warning or notice is my scenario.)


Solution

  • The reason the program quits is because Symfony promotes the warning to an exception, and nothing in your code catches it. You can prevent this behaviour by adding the command line option --no-debug when you call your command.