<?php
namespace App\EventSubscriber;
use App\Command\Impl\AbstractCronCommand;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Auto flushes objects before HTTP responses
*/
class CommandErrorEventSubscriber implements EventSubscriberInterface
{
/** @inheritdoc */
public static function getSubscribedEvents()
{
return [
ConsoleEvents::ERROR => ['commandError', 1024]
];
}
public function commandError(ConsoleErrorEvent $event): void
{
if ($event->getCommand() instanceof AbstractCronCommand) {
$io = new SymfonyStyle($event->getInput(), $event->getOutput());
if (0 === $event->getError()->getCode()) {
$event->setExitCode(99);
}
$error = $event->getError();
$io->error(sprintf("Error with message : %s. In file %s line %s. StackTrace : %s.", $error->getMessage(), $error->getFile(), $error->getLine(), $error->getTraceAsString()));
}
}
}