From a12b655cb4010581d6938b10de8ae462f3c6c580 Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Fri, 18 Dec 2020 23:07:28 -0600 Subject: [PATCH] Log console exceptions and nonzero exit codes. --- bin/console | 9 +++-- config/events.php | 42 +++++++++++++--------- config/services.php | 1 + src/Console/ErrorHandler.php | 68 ++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 src/Console/ErrorHandler.php diff --git a/bin/console b/bin/console index f47cf636b..00cbee9e5 100644 --- a/bin/console +++ b/bin/console @@ -5,9 +5,12 @@ ini_set('display_errors', 1); $autoloader = require dirname(__DIR__) . '/vendor/autoload.php'; -$app = App\AppFactory::create($autoloader, [ - App\Environment::BASE_DIR => dirname(__DIR__), -]); +$app = App\AppFactory::create( + $autoloader, + [ + App\Environment::BASE_DIR => dirname(__DIR__), + ] +); $di = $app->getContainer(); diff --git a/config/events.php b/config/events.php index b14fa6a9b..68a18514f 100644 --- a/config/events.php +++ b/config/events.php @@ -48,7 +48,9 @@ return function (App\EventDispatcher $dispatcher) { $migrationConfigurations = $buildMigrationConfigurationsEvent->getMigrationConfigurations(); - $migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray($migrationConfigurations); + $migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray( + $migrationConfigurations + ); $migrateFactory = Doctrine\Migrations\DependencyFactory::fromEntityManager( $migrateConfig, @@ -113,13 +115,19 @@ return function (App\EventDispatcher $dispatcher) { ); // Build default menus - $dispatcher->addListener(App\Event\BuildAdminMenu::class, function (App\Event\BuildAdminMenu $e) { - call_user_func(include(__DIR__ . '/menus/admin.php'), $e); - }); + $dispatcher->addListener( + App\Event\BuildAdminMenu::class, + function (App\Event\BuildAdminMenu $e) { + call_user_func(include(__DIR__ . '/menus/admin.php'), $e); + } + ); - $dispatcher->addListener(App\Event\BuildStationMenu::class, function (App\Event\BuildStationMenu $e) { - call_user_func(include(__DIR__ . '/menus/station.php'), $e); - }); + $dispatcher->addListener( + App\Event\BuildStationMenu::class, + function (App\Event\BuildStationMenu $e) { + call_user_func(include(__DIR__ . '/menus/station.php'), $e); + } + ); // Other event subscribers from across the application. $dispatcher->addCallableListener( @@ -144,13 +152,15 @@ return function (App\EventDispatcher $dispatcher) { App\Notification\Check\SyncTaskCheck::class ); - $dispatcher->addServiceSubscriber([ - App\Radio\AutoDJ\Queue::class, - App\Radio\AutoDJ\Annotations::class, - App\Radio\Backend\Liquidsoap\ConfigWriter::class, - App\Sync\Task\NowPlayingTask::class, - App\Webhook\Dispatcher::class, - App\Controller\Api\NowplayingController::class, - ]); - + $dispatcher->addServiceSubscriber( + [ + App\Console\ErrorHandler::class, + App\Radio\AutoDJ\Queue::class, + App\Radio\AutoDJ\Annotations::class, + App\Radio\Backend\Liquidsoap\ConfigWriter::class, + App\Sync\Task\NowPlayingTask::class, + App\Webhook\Dispatcher::class, + App\Controller\Api\NowplayingController::class, + ] + ); }; diff --git a/config/services.php b/config/services.php index 68c0884f4..b1cf0e9ab 100644 --- a/config/services.php +++ b/config/services.php @@ -216,6 +216,7 @@ return [ // Console App\Console\Application::class => function (DI\Container $di, App\EventDispatcher $dispatcher) { $console = new App\Console\Application('Command Line Interface', '1.0.0', $di); + $console->setDispatcher($dispatcher); // Trigger an event for the core app and all plugins to build their CLI commands. $event = new App\Event\BuildConsoleCommands($console); diff --git a/src/Console/ErrorHandler.php b/src/Console/ErrorHandler.php new file mode 100644 index 000000000..5515af9bc --- /dev/null +++ b/src/Console/ErrorHandler.php @@ -0,0 +1,68 @@ +logger = $logger; + } + + /** + * @return mixed[] + */ + public static function getSubscribedEvents(): array + { + return [ + ConsoleEvents::TERMINATE => [ + 'onTerminate', + ], + ConsoleEvents::ERROR => [ + 'onError', + ], + ]; + } + + public function onTerminate(ConsoleTerminateEvent $event): void + { + $command = $event->getCommand(); + + $exitCode = $event->getExitCode(); + if (0 === $exitCode) { + return; + } + + $message = sprintf( + 'Console command `%s` exited with error code %d.', + $command->getName(), + $exitCode + ); + $this->logger->warning($message); + } + + public function onError(ConsoleErrorEvent $event): void + { + $command = $event->getCommand(); + $exception = $event->getError(); + + $message = sprintf( + '%s: %s (uncaught exception) at %s line %s while running console command `%s`', + get_class($exception), + $exception->getMessage(), + $exception->getFile(), + $exception->getLine(), + $command->getName() + ); + + $this->logger->error($message, ['exception' => $exception]); + } +}