From 39a19bdf478583f9b63a2b1a98746a713e217c86 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 9 Oct 2016 22:39:43 +0200 Subject: [PATCH] When a sub command fail, display error message MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit We often got issue with message “The command "doctrine:database:create" terminated with an error code: 1.”. Using the `BufferedOutput` we can store the output and only display it if an error occurs. --- .../CoreBundle/Command/InstallCommand.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index cc7c2c94..8e438229 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -9,7 +9,7 @@ use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; @@ -97,7 +97,8 @@ class InstallCommand extends ContainerAwareCommand try { $this->getContainer()->get('doctrine')->getManager()->getConnection()->connect(); } catch (\Exception $e) { - if (false === strpos($e->getMessage(), 'Unknown database')) { + if (false === strpos($e->getMessage(), 'Unknown database') + && false === strpos($e->getMessage(), 'database "'.$this->getContainer()->getParameter('database_name').'" does not exist')) { $fulfilled = false; $status = 'ERROR!'; $help = 'Can\'t connect to the database: '.$e->getMessage(); @@ -420,16 +421,18 @@ class InstallCommand extends ContainerAwareCommand } $this->getApplication()->setAutoExit(false); - $exitCode = $this->getApplication()->run(new ArrayInput($parameters), new NullOutput()); + + $output = new BufferedOutput(); + $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); if (0 !== $exitCode) { $this->getApplication()->setAutoExit(true); - $errorMessage = sprintf('The command "%s" terminated with an error code: %u.', $command, $exitCode); - $this->defaultOutput->writeln("$errorMessage"); - $exception = new \Exception($errorMessage, $exitCode); + $this->defaultOutput->writeln(''); + $this->defaultOutput->writeln('The command "'.$command.'" generates some errors: '); + $this->defaultOutput->writeln($output->fetch()); - throw $exception; + die(); } // PDO does not always close the connection after Doctrine commands. -- 2.41.0