Skip to content

Commit 26ad4ff

Browse files
committed
feature symfony#17504 [Console] Show code when an exception is thrown (maidmaid)
This PR was squashed before being merged into the 3.1-dev branch (closes symfony#17504). Discussion ---------- [Console] Show code when an exception is thrown | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Hi ! When an exception is thrown, sf console doesn't show the code exception, while this code is very useful during developpement (for example, [guzzle put http status code in his code exception](https://github.com/guzzle/guzzle/blob/6.1.1/src/Exception/RequestException.php#L30-L33)). I propose you that we show the code exception only when the code exception > 0 and with ``-v`` verbosity. It means : - ``throw new Exception('Not found', 0)`` with normal verbosity --> no change - ``throw new Exception('Not found', 0)`` with ``-v`` verbosity --> no change - ``throw new Exception('Not found', 404)`` with normal verbosity --> no change - **but** ``throw new Exception('Not found', 404)`` with ``-v`` verbosity --> showing : ![image](https://cloud.githubusercontent.com/assets/4578773/12530638/052991f2-c1e5-11e5-92f1-b7b3f60cc4ba.png) Commits ------- f8cd9e8 [Console] Show code when an exception is thrown
2 parents 1525cce + f8cd9e8 commit 26ad4ff

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)