From 886b7edcf626a08417d830cdc09d7ab31f0f5a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Tue, 3 Jan 2017 18:12:58 -0300 Subject: [PATCH] [Nostromo] Add Exception Listener for uncaught exceptions in Command, wrap Bin in exception for early errors. --- bin/mage | 11 ++++++++--- src/Mage/MageApplication.php | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/bin/mage b/bin/mage index 162867f..26cd158 100755 --- a/bin/mage +++ b/bin/mage @@ -10,6 +10,11 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) { use Mage\MageApplication; -$mage = new MageApplication(); -$mage->configure('.mage.yml'); -$mage->run(); \ No newline at end of file +try { + $mage = new MageApplication(); + $mage->configure('.mage.yml'); + $mage->run(); +} catch (Exception $exception) { + printf('Error: %s' . PHP_EOL, $exception->getMessage()); + exit(9); +} diff --git a/src/Mage/MageApplication.php b/src/Mage/MageApplication.php index 5a3553f..064019c 100644 --- a/src/Mage/MageApplication.php +++ b/src/Mage/MageApplication.php @@ -18,6 +18,9 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; use Monolog\Logger; use Monolog\Handler\StreamHandler; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\Console\Event\ConsoleExceptionEvent; +use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\Console\Application; use Symfony\Component\Yaml\Yaml; use Mage\Runtime\Exception\RuntimeException; @@ -31,6 +34,22 @@ class MageApplication extends Application { protected $runtime; + public function __construct() + { + $dispatcher = new EventDispatcher(); + + $dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) { + $output = $event->getOutput(); + $command = $event->getCommand(); + $output->writeln(sprintf('Oops, exception thrown while running command %s', $command->getName())); + $exitCode = $event->getExitCode(); + $event->setException(new \LogicException('Caught exception', $exitCode, $event->getException())); + }); + + $this->setDispatcher($dispatcher); + parent::__construct('Magallanes', Mage::VERSION); + } + /** * Configure the Magallanes Application *