[Nostromo] Add Exception Listener for uncaught exceptions in Command, wrap Bin in exception for early errors.

This commit is contained in:
Andrés Montañez 2017-01-03 18:12:58 -03:00
parent 2c5e44ab1e
commit 886b7edcf6
2 changed files with 27 additions and 3 deletions

View file

@ -10,6 +10,11 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) {
use Mage\MageApplication;
$mage = new MageApplication();
$mage->configure('.mage.yml');
$mage->run();
try {
$mage = new MageApplication();
$mage->configure('.mage.yml');
$mage->run();
} catch (Exception $exception) {
printf('Error: %s' . PHP_EOL, $exception->getMessage());
exit(9);
}

View file

@ -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 <info>%s</info>', $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
*