Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 340
user3541554 Avatar asked Oct 31 '22 11:10

user3541554


1 Answers

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.

like image 66
user2634914 Avatar answered Nov 14 '22 13:11

user2634914