8000 [Console] Revised exception rendering · symfony/symfony@17f1f07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17f1f07

Browse files
ro0NLfabpot
authored andcommitted
[Console] Revised exception rendering
1 parent dd37126 commit 17f1f07

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -650,28 +650,27 @@ public function renderException($e, $output)
650650
if (defined('HHVM_VERSION') && $width > 1 << 31) {
651651
$width = 1 << 31;
652652
}
653-
$formatter = $output->getFormatter();
654653
$lines = array();
655-
foreach (preg_split('/\r?\n/', OutputFormatter::escape($e->getMessage())) as $line) {
654+
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
656655
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
657656
// pre-format lines to get the right string length
658-
$lineLength = $this->stringWidth(preg_replace('/\[[^m]*m/', '', $formatter->format($line))) + 4;
657+
$lineLength = $this->stringWidth($line) + 4;
659658
$lines[] = array($line, $lineLength);
660659

661660
$len = max($lineLength, $len);
662661
}
663662
}
664663

665664
$messages = array();
666-
$messages[] = $emptyLine = $formatter->format(sprintf('<error>%s</error>', str_repeat(' ', $len)));
667-
$messages[] = $formatter->format(sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title)))));
665+
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
666+
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title))));
668667
foreach ($lines as $line) {
669-
$messages[] = $formatter->format(sprintf('<error> %s %s</error>', $line[0], str_repeat(' ', $len - $line[1])));
668+
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
670669
}
671670
$messages[] = $emptyLine;
672671
$messages[] = '';
673672

674-
$output->writeln($messages, OutputInterface::OUTPUT_RAW);
673+
$output->writeln($messages);
675674

676675
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
677676
$output->writeln('<comment>Exception trace:</comment>');

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,22 @@ public function testRenderExceptionWithDoubleWidthCharacters()
591591
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getDisplay(true), '->renderException() wraps messages when they are bigger than the terminal');
592592
}
593593

594+
public function testRenderExceptionEscapesLines()
595+
{
596+
$application = $this->getMockBuilder('Symfony\Component\Console\Application')->setMethods(array('getTerminalWidth'))->getMock();
597+
$application->setAutoExit(false);
598+
$application->expects($this->any())
599+
->method('getTerminalWidth')
600+
->will($this->returnValue(22));
601+
$application->register('foo')->setCode(function () {
602+
throw new \Exception('dont break here <info>!</info>');
603+
});
604+
$tester = new ApplicationTester($application);
605+
606+
$tester->run(array('command' => 'foo'), array('decorated' => false));
607+
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_escapeslines.txt', $tester->getDisplay(true), '->renderException() escapes lines containing formatting');
608+
}
609+
594610
public function testRun()
595611
{
596612
$application = new Application();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
[Exception]
4+
dont break here <
5+
info>!</info>
6+
7+
8+
foo
9+

0 commit comments

Comments
 (0)
0