Skip to content

Commit

Permalink
[Console] Show code when an exception is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
maidmaid authored and fabpot committed Jan 27, 2016
1 parent ed2cdfa commit f8cd9e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Console/Application.php
Expand Up @@ -588,7 +588,11 @@ public function renderException(\Exception $e, OutputInterface $output)
$output->writeln('', OutputInterface::VERBOSITY_QUIET);

do {
$title = sprintf(' [%s] ', get_class($e));
$title = sprintf(
' [%s%s] ',
get_class($e),
$output->isVerbose() && 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : ''
);

$len = $this->stringWidth($title);

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -520,6 +520,11 @@ public function testRenderException()
$tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');

$tester->run(array('command' => 'foo3:bar'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
$this->assertRegExp('/\[Exception\]\s*First exception/', $tester->getDisplay(), '->renderException() renders a pretty exception without code exception when code exception is default and verbosity is verbose');
$this->assertRegExp('/\[Exception\]\s*Second exception/', $tester->getDisplay(), '->renderException() renders a pretty exception without code exception when code exception is 0 and verbosity is verbose');
$this->assertRegExp('/\[Exception \(404\)\]\s*Third exception/', $tester->getDisplay(), '->renderException() renders a pretty exception with code exception when code exception is 404 and verbosity is verbose');

$tester->run(array('command' => 'foo3:bar'), array('decorated' => true));
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');

Expand Down
Expand Up @@ -23,7 +23,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \Exception('Second exception <comment>comment</comment>', 0, $e);
}
} catch (\Exception $e) {
throw new \Exception('Third exception <fg=blue;bg=red>comment</>', 0, $e);
throw new \Exception('Third exception <fg=blue;bg=red>comment</>', 404, $e);
}
}
}

0 comments on commit f8cd9e8

Please sign in to comment.