Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b2e1415

Browse filesBrowse files
author
Robin Chalas
committed
feature #21414 [Console] Display file and line on Exception (arno14)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Display file and line on Exception | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | - | License | MIT | Doc PR | - When an exception occured: If you have not set a verbose mode when running the command, you don't have information about the file and the line on which the exception occured. So you must run a second time the command but the exception may not occured this time (if based on some databases datas which have changed) and it's a waste of time if you process is very long and the exception occured at the end. The feature #21003 solve the problem if you are in a standard Symfony edition, if you leave the default settings and if you are in dev mode Commits ------- 484d278 [Console] Display file and line on Exception
2 parents c7e61f2 + 484d278 commit b2e1415
Copy full SHA for b2e1415

10 files changed

+35
-38
lines changed

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+12-15Lines 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'] : '';
+4-4Lines 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

+4-4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt
+3-3Lines 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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt
+3-3Lines 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

+5-5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1.txt
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1decorated.txt
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth2.txt
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_escapeslines.txt
+1-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.