From 484d278f50e740c72224db7af94c7342be382331 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Thu, 26 Jan 2017 11:39:46 +0100 Subject: [PATCH] [Console] Display file and line on Exception --- src/Symfony/Component/Console/Application.php | 27 +++++++++---------- .../Fixtures/application_renderexception1.txt | 8 +++--- .../Fixtures/application_renderexception2.txt | 8 +++--- .../Fixtures/application_renderexception3.txt | 6 ++--- .../application_renderexception3decorated.txt | 6 ++--- .../Fixtures/application_renderexception4.txt | 10 +++---- ...plication_renderexception_doublewidth1.txt | 2 +- ..._renderexception_doublewidth1decorated.txt | 2 +- ...plication_renderexception_doublewidth2.txt | 2 +- ...plication_renderexception_escapeslines.txt | 2 +- 10 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index c1731843e6..41c2a238e0 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -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('%s', OutputFormatter::escape(sprintf('In %s line %s:', basename($e->getFile()) ?: 'n/a', $e->getLine() ?: 'n/a'))); $messages[] = $emptyLine = sprintf('%s', str_repeat(' ', $len)); - $messages[] = sprintf('%s%s', $title, str_repeat(' ', max(0, $len - Helper::strlen($title)))); + if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { + $messages[] = sprintf('%s%s', $title, str_repeat(' ', max(0, $len - Helper::strlen($title)))); + } foreach ($lines as $line) { $messages[] = sprintf(' %s %s', 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'] : ''; diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception1.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception1.txt index 919cec4214..700ffada3d 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception1.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception1.txt @@ -1,6 +1,6 @@ - - [Symfony\Component\Console\Exception\CommandNotFoundException] - Command "foo" is not defined. - +In Application.php line 615: + + Command "foo" is not defined. + diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt index 64561715e0..9d0cacb81a 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt @@ -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] [--] [] diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt index f41925f52a..5366b84f82 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3.txt @@ -1,16 +1,16 @@ +In Foo3Command.php line 26: - [Exception] Third exception comment +In Foo3Command.php line 23: - [Exception] Second exception comment +In Foo3Command.php line 21: - [Exception] First exception

this is html

diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt index 5adccdd702..59937092e7 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception3decorated.txt @@ -1,16 +1,16 @@ +In Foo3Command.php line 26:   - [Exception]   Third exception comment    +In Foo3Command.php line 23:   - [Exception]   Second exception comment    +In Foo3Command.php line 21:   - [Exception]   First exception

this is html

   diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception4.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception4.txt index cb080e9cb5..040564447f 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception4.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception4.txt @@ -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. + diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1.txt index 1ba5f8fdd9..01986c917b 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1.txt @@ -1,6 +1,6 @@ +In ApplicationTest.php line 716: - [Exception] エラーメッセージ diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1decorated.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1decorated.txt index 20644251c2..ba057d8cc7 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1decorated.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth1decorated.txt @@ -1,6 +1,6 @@ +In ApplicationTest.php line 716:   - [Exception]   エラーメッセージ    diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth2.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth2.txt index e41fcfcf67..66b52c879a 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth2.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_doublewidth2.txt @@ -1,6 +1,6 @@ +In ApplicationTest.php line 730: - [Exception] コマンドの実行中にエラーが 発生しました。 diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_escapeslines.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_escapeslines.txt index cf79b37a92..326ee42f6d 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_escapeslines.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_renderexception_escapeslines.txt @@ -1,6 +1,6 @@ +In ApplicationTest.php line 744: - [Exception] dont break here < info>!