Skip to content

[Console] Display file and line on Exception #21414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,21 +736,21 @@ public function renderException(\Exception $e, OutputInterface $output)
protected function doRenderException(\Exception $e, OutputInterface $output)
{
do {
$title = sprintf(
' [%s%s] ',
get_class($e),
$output->isVerbose() && 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : ''
);

$len = Helper::strlen($title);
$message = $e->getMessage();
if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
$title = sprintf(' [%s%s] ', get_class($e), 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : '');
$len = Helper::strlen($title);
} else {
$len = 0;
}

$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
// 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
if (defined('HHVM_VERSION') && $width > 1 << 31) {
$width = 1 << 31;
}
$lines = array();
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
foreach ('' !== $message ? preg_split('/\r?\n/', $message) : array() as $line) {
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
// pre-format lines to get the right string length
$lineLength = Helper::strlen($line) + 4;
Expand All @@ -761,8 +761,11 @@ protected function doRenderException(\Exception $e, OutputInterface $output)
}

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

// exception related properties
$trace = $e->getTrace();
array_unshift($trace, array(
'function' => '',
'file' => $e->getFile() !== null ? $e->getFile() : 'n/a',
'line' => $e->getLine() !== null ? $e->getLine() : 'n/a',
'args' => array(),
));

for ($i = 0, $count = count($trace); $i < $count; ++$i) {
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "foo" is not defined.
In Application.php line 615:
Command "foo" is not defined.


Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

[Symfony\Component\Console\Exception\InvalidOptionException]
The "--foo" option does not exist.
In ArrayInput.php line 172:
The "--foo" option does not exist.


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

Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

In Foo3Command.php line 26:

[Exception]
Third exception <fg=blue;bg=red>comment</>


In Foo3Command.php line 23:

[Exception]
Second exception <comment>comment</comment>


In Foo3Command.php line 21:

[Exception]
First exception <p>this is html</p>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

In Foo3Command.php line 26:
 
 [Exception] 
 Third exception <fg=blue;bg=red>comment</> 
 

In Foo3Command.php line 23:
 
 [Exception] 
 Second exception <comment>comment</comment> 
 

In Foo3Command.php line 21:
 
 [Exception] 
 First exception <p>this is html</p> 
 

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "foo" is not define
d.
In Application.php line 615:
Command "foo" is not define
d.


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

In ApplicationTest.php line 716:

[Exception]
エラーメッセージ


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

In ApplicationTest.php line 716:
 
 [Exception] 
 エラーメッセージ 
 

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

In ApplicationTest.php line 730:

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

In ApplicationTest.php line 744:

[Exception]
dont break here <
info>!</info>

Expand Down