8000 [Console] Show code when an exception is thrown · symfony/symfony@f8cd9e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit f8cd9e8

Browse files
maidmaidfabpot
authored andcommitted
[Console] Show code when an exception is thrown
1 parent ed2cdfa commit f8cd9e8

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,11 @@ public function renderException(\Exception $e, OutputInterface $output)
588588
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
589589

590590
do {
591-
$title = sprintf(' [%s] ', get_class($e));
591+
$title = sprintf(
592+
' [%s%s] ',
593+
get_class($e),
594+
$output->isVerbose() && 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : ''
595+
);
592596

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

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ public function testRenderException()
520520
$tester->run(array('command' => 'foo3:bar'), array('decorated' => false));
521521
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
522522

523+
$tester->run(array('command' => 'foo3:bar'), array('decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
524+
$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');
525+
$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');
526+
$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');
527+
523528
$tester->run(array('command' => 'foo3:bar'), array('decorated' => true));
524529
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3decorated.txt', $tester->getDisplay(true), '->renderException() renders a pretty exceptions with previous exceptions');
525530

src/Symfony/Component/Console/Tests/Fixtures/Foo3Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
2323
throw new \Exception('Second exception <comment>comment</comment>', 0, $e);
2424
}
2525
} catch (\Exception $e) {
26-
throw new \Exception('Third exception <fg=blue;bg=red>comment</>', 0, $e);
26+
throw new \Exception('Third exception <fg=blue;bg=red>comment</>', 404, $e);
2727
}
2828
}
2929
}

0 commit comments

Comments
 (0)
0