src/EventSubscriber/CommandErrorEventSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Command\Impl\AbstractCronCommand;
  4. use Symfony\Component\Console\ConsoleEvents;
  5. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  6. use Symfony\Component\Console\Style\SymfonyStyle;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9.  * Auto flushes objects before HTTP responses
  10.  */
  11. class CommandErrorEventSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @inheritdoc */
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             ConsoleEvents::ERROR => ['commandError'1024]
  18.         ];
  19.     }
  20.     public function commandError(ConsoleErrorEvent $event): void
  21.     {
  22.         if ($event->getCommand() instanceof AbstractCronCommand) {
  23.             $io = new SymfonyStyle($event->getInput(), $event->getOutput());
  24.             if (=== $event->getError()->getCode()) {
  25.                 $event->setExitCode(99);
  26.             }
  27.             $error $event->getError();
  28.             $io->error(sprintf("Error with message : %s. In file %s line %s. StackTrace : %s."$error->getMessage(), $error->getFile(), $error->getLine(), $error->getTraceAsString()));
  29.         }
  30.     }
  31. }