How to prevent Symfony from crashing your entire program when you receive a warning or notification

In Symfony, when we run a command, if there are any notifications or warnings, Symfony displays a large red message and exits the entire program.

I want an error to be logged, but I do not want the entire program to terminate. Can someone please tell me how to do 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 notification is my script.)

+5
source share
2 answers

The reason the program terminates is because Symfony pushes the warning to an exception, and nothing in the code catches it. This can be prevented by adding the command line parameter --no-debug when calling your command.

0
source

Unfortunately, these errors cannot be caught because they are no exception.

However, since they are errors, your first step should be to attempt to correct these errors.

Sometimes, however, these errors come from code that you call from third-party libraries that you cannot influence. In this case, you can simply prefix the function call that causes the error with "@". This will suppress these messages.

Alternatively, you can check the error_reporting and display_error settings of your cli 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.

0
source

Source: https://habr.com/ru/post/1211723/


All Articles