7

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

user3541554
  • 137
  • 1
  • 6

2 Answers2

2

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.

1

Unfortunately, there is no way to catch these errors, since they are no exceptions.

However, since they are errors, your first action should be to try to fix these errors.

Sometimes however, these errors come from code you call from third party libraries, which you cant effect. In this case, you could just prefix a function call that triggers an error with an "@". This will surpress these messages.

Alternativly, you might want to check the error_reporting and display_error settings of your cli's php.ini:

http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors http://php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

Hope this helps.