8000 [Console] Display file and line on Exception · symfony/symfony@484d278 · GitHub
[go: up one dir, main page]

Skip to content

Commit 484d278

Browse files
arnaudcogifornicolas-grekas
authored andcommitted
[Console] Display file and line on Exception
1 parent dd3276c commit 484d278

10 files changed

+35
-38
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -736,21 +736,21 @@ public function renderException(\Exception $e, OutputInterface $output)
736736
protected function doRenderException(\Exception $e, OutputInterface $output)
737737
{
738738
do {
739-
$title = sprintf(
740-
' [%s%s] ',
741-
get_class($e),
742-
$output->isVerbose() && 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : ''
743-
);
744-
745-
$len = Helper::strlen($title);
739+
$message = $e->getMessage();
740+
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
741+
$title = sprintf(' [%s%s] ', get_class($e), 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
742+
$len = Helper::strlen($title);
743+
} else {
744+
$len = 0;
745+
}
746746

747747
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
748748
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
749749
if (defined('HHVM_VERSION') && $width > 1 << 31) {
750750
$width = 1 << 31;
751751
}
752752
$lines = array();
753-
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
753+
foreach ('' !== $message ? preg_split('/\r?\n/', $message) : array() as $line) {
754754
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
755755
// pre-format lines to get the right string length
756756
$lineLength = Helper::strlen($line) + 4;
@@ -761,8 +761,11 @@ protected function doRenderException(\Exception $e, OutputInterface $output)
761761
}
762762

763763
$messages = array();
764+
$messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape(sprintf('In %s line %s:', basename($e->getFile()) ?: 'n/a', $e->getLine() ?: 'n/a')));
764765
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
765-
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
766+
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
767+
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
768+
}
766769
foreach ($lines as $line) {
767770
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
768771
}
@@ -776,12 +779,6 @@ protected function doRenderException(\Exception $e, OutputInterface $output)
776779

777780
// exception related properties
778781
$trace = $e->getTrace();
779-
array_unshift($trace, array(
780-
'function' => '',
781-
'file' => $e->getFile() !== null ? $e->getFile() : 'n/a',
782-
'line' => $e->getLine() !== null ? $e->getLine() : 'n/a',
783-
'args' => array(),
784-
));
785782

786783
for ($i = 0, $count = count($trace); $i < $count; ++$i) {
787784
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
3-
[Symfony\Component\Console\Exception\CommandNotFoundException]
4-
Command "foo" is not defined.
5-
2+
In Application.php line 615:
3+
4+
Command "foo" is not defined.
5+
66

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
3-
[Symfony\Component\Console\Exception\InvalidOptionException]
4-
The "--foo" option does not exist.
5-
2+
In ArrayInput.php line 172:
3+
4+
The "--foo" option does not exist.
5+
66

77
list [--raw] [--format FORMAT] [--] [<namespace>]
88

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt

Lines changed: 3 additions & 3 deletions
< F438 /tr>
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

2+
In Foo3Command.php line 26:
23

3-
[Exception]
44
Third exception <fg=blue;bg=red>comment</>
55

66

7+
In Foo3Command.php line 23:
78

8-
[Exception]
99
Second exception <comment>comment</comment>
1010

1111

12+
In Foo3Command.php line 21:
1213

13-
[Exception]
1414
First exception <p>this is html</p>
1515

1616

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

2+
In Foo3Command.php line 26:
23
 
3-
 [Exception] 
44
 Third exception <fg=blue;bg=red>comment</> 
55
 
66

7+
In Foo3Command.php line 23:
78
 
8-
 [Exception] 
99
 Second exception <comment>comment</comment> 
1010
 
1111

12+
In Foo3Command.php line 21:
1213
 
13-
 [Exception] 
1414
 First exception <p>this is html</p> 
1515
 
1616

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
3-
[Symfony\Component\Console\Exception\CommandNotFoundException]
4-
Command "foo" is not define
5-
d.
6-
2+
In Application.php line 615:
3+
4+
Command "foo" is not define
5+
d.
6+
77

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2+
In ApplicationTest.php line 716:
23

3-
[Exception]
44
エラーメッセージ
55

66

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1decorated.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2+
In ApplicationTest.php line 716:
23
 
3-
 [Exception] 
44
 エラーメッセージ 
55
 
66

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2+
In ApplicationTest.php line 730:
23

3-
[Exception]
44
コマンドの実行中にエラーが
55
発生しました。
66

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_escapeslines.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2+
In ApplicationTest.php line 744:
23

3-
[Exception]
44
dont break here <
55
info>!</info>
66

0 commit comments

Comments
 (0)
0