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
This commit is contained in:
Robin Chalas 2017-09-03 17:31:40 +02:00
commit b2e1415cab
10 changed files with 35 additions and 38 deletions

View File

@ -736,13 +736,13 @@ class Application
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
@ -750,7 +750,7 @@ class Application
$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;
@ -761,8 +761,11 @@ class Application
}
$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]));
}
@ -776,12 +779,6 @@ class Application
// 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'] : '';

View File

@ -1,6 +1,6 @@
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "foo" is not defined.
In Application.php line 615:
Command "foo" is not defined.

View File

@ -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>]

View File

@ -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>

View File

@ -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> 
 

View File

@ -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.

View File

@ -1,6 +1,6 @@
In ApplicationTest.php line 716:
[Exception]
エラーメッセージ

View File

@ -1,6 +1,6 @@
In ApplicationTest.php line 716:
 
 [Exception] 
 エラーメッセージ 
 

View File

@ -1,6 +1,6 @@
In ApplicationTest.php line 730:
[Exception]
コマンドの実行中にエラーが
発生しました。

View File

@ -1,6 +1,6 @@
In ApplicationTest.php line 744:
[Exception]
dont break here <
info>!</info>