Skip to content
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

[TASK] Refactor Exception renderer #690

Merged
merged 1 commit into from
Mar 14, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Classes/Console/Core/Booting/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function removeStep($stepIdentifier)
{
$removedOccurrences = 0;
foreach ($this->steps as $previousStepIdentifier => $steps) {
// TODO: Report this bugfix to upstream!
foreach ($steps as $index => $step) {
if ($step->getIdentifier() === $stepIdentifier) {
unset($this->steps[$previousStepIdentifier][$index]);
Expand Down
110 changes: 60 additions & 50 deletions Classes/Console/Error/ExceptionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function render(\Throwable $exception, OutputInterface $output, Applicati
do {
$this->outputException($exception, $output);
if ($output->isVerbose()) {
$this->outputCode($exception, $output);
$this->outputCommand($exception, $output);
$this->outputTrace($exception, $output);
$output->writeln('');
}
Expand All @@ -72,7 +74,6 @@ public function render(\Throwable $exception, OutputInterface $output, Applicati
*/
private function outputException(\Throwable $exception, OutputInterface $output)
{
$exceptionCodeNumber = $exception->getCode() > 0 ? $exception->getCode() : '';
$exceptionClass = get_class($exception);
if ($exception instanceof SubProcessException) {
$exceptionClass = $exception->getPreviousExceptionClass();
Expand Down Expand Up @@ -100,61 +101,41 @@ private function outputException(\Throwable $exception, OutputInterface $output)
$messages[] = $emptyLine;
$messages[] = '';
$output->writeln($messages, OutputInterface::VERBOSITY_QUIET);

if ($output->isVerbose()) {
if ($exceptionCodeNumber) {
$output->writeln(sprintf('<comment>Exception code:</comment> <info>%s</info>', $exceptionCodeNumber));
$output->writeln('');
}

if ($exception instanceof FailedSubProcessCommandException
|| ($exception instanceof SubProcessException && $exception->getCommandLine())
) {
$output->writeln('<comment>Command line:</comment>');
$output->writeln($exception->getCommandLine());
$output->writeln('');
if ($exception->getOutputMessage()) {
$output->writeln('<comment>Command output:</comment>');
$output->writeln($exception->getOutputMessage());
$output->writeln('');
}
if ($exception->getErrorMessage()) {
$output->writeln('<comment>Command error output:</comment>');
$output->writeln($exception->getErrorMessage());
$output->writeln('');
}
}
}
}

private function splitStringByWidth($string, $width)
/**
* @param \Throwable $exception
* @param OutputInterface $output
*/
private function outputCode(\Throwable $exception, OutputInterface $output)
{
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
// additionally, array_slice() is not enough as some character has doubled width.
// we need a function to split string not by character count but by string width
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return str_split($string, $width);
if ($exception->getCode() > 0) {
$output->writeln(sprintf('<comment>Exception code:</comment> <info>%s</info>', $exception->getCode()));
$output->writeln('');
}
}

$utf8String = mb_convert_encoding($string, 'utf8', $encoding);
$lines = [];
$line = '';
foreach (preg_split('//u', $utf8String) as $char) {
// test if $char could be appended to current line
if (mb_strwidth($line . $char, 'utf8') <= $width) {
$line .= $char;
continue;
/**
* @param \Throwable $exception
* @param OutputInterface $output
*/
private function outputCommand(\Throwable $exception, OutputInterface $output)
{
if ($exception instanceof FailedSubProcessCommandException || ($exception instanceof SubProcessException && $exception->getCommandLine())) {
$output->writeln('<comment>Command line:</comment>');
$output->writeln($exception->getCommandLine());
$output->writeln('');
if ($exception->getOutputMessage()) {
$output->writeln('<comment>Command output:</comment>');
$output->writeln($exception->getOutputMessage());
$output->writeln('');
}
if ($exception->getErrorMessage()) {
$output->writeln('<comment>Command error output:</comment>');
$output->writeln($exception->getErrorMessage());
$output->writeln('');
}
// if not, push current line to array and make new line
$lines[] = str_pad($line, $width);
$line = $char;
}

$lines[] = count($lines) ? str_pad($line, $width) : $line;

mb_convert_variables($encoding, 'utf8', $lines);

return $lines;
}

/**
Expand All @@ -172,7 +153,7 @@ private function outputTrace(\Throwable $exception, OutputInterface $output)
$traceLine .= $backtraceSteps[$index]['class'];
}
if (isset($backtraceSteps[$index]['function'])) {
$traceLine .= (isset($backtraceSteps[$index]['class']) ? '::' : '') . $backtraceSteps[$index]['function'] . '()';
$traceLine .= (isset($backtraceSteps[$index]['class']) ? $backtraceSteps[$index]['type'] : '') . $backtraceSteps[$index]['function'] . '()';
}
$output->writeln(sprintf('<info>%s</info>', $traceLine));
if (isset($backtraceSteps[$index]['file'])) {
Expand All @@ -197,6 +178,35 @@ private function outputSynopsis(OutputInterface $output, Application $applicatio
}
}

private function splitStringByWidth($string, $width)
{
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
// additionally, array_slice() is not enough as some character has doubled width.
// we need a function to split string not by character count but by string width
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return str_split($string, $width);
}

$utf8String = mb_convert_encoding($string, 'utf8', $encoding);
$lines = [];
$line = '';
foreach (preg_split('//u', $utf8String) as $char) {
// test if $char could be appended to current line
if (mb_strwidth($line . $char, 'utf8') <= $width) {
$line .= $char;
continue;
}
// if not, push current line to array and make new line
$lines[] = str_pad($line, $width);
$line = $char;
}

$lines[] = count($lines) ? str_pad($line, $width) : $line;
mb_convert_variables($encoding, 'utf8', $lines);

return $lines;
}

/**
* Right pad message.
*
Expand Down
12 changes: 6 additions & 6 deletions Classes/Console/Mvc/Cli/FailedSubProcessCommandException.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function forProcess($command, Process $process)
*/
public function __construct($command, $commandLine, $exitCode, $outputMessage, $errorMessage)
{
$previousExceptionData = @json_decode($errorMessage, true) ?: null;
$previousExceptionData = @\json_decode($errorMessage, true) ?: null;
$previousException = null;
if ($previousExceptionData) {
$errorMessage = '';
Expand All @@ -92,39 +92,39 @@ public function __construct($command, $commandLine, $exitCode, $outputMessage, $
/**
* @return string
*/
public function getCommand()
public function getCommand(): string
{
return $this->command;
}

/**
* @return string
*/
public function getCommandLine()
public function getCommandLine(): string
{
return $this->commandLine;
}

/**
* @return int
*/
public function getExitCode()
public function getExitCode(): int
{
return $this->exitCode;
}

/**
* @return string
*/
public function getOutputMessage()
public function getOutputMessage(): string
{
return $this->outputMessage;
}

/**
* @return string
*/
public function getErrorMessage()
public function getErrorMessage(): string
{
return $this->errorMessage;
}
Expand Down
6 changes: 3 additions & 3 deletions Classes/Console/Mvc/Cli/SubProcessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class SubProcessException extends \Exception
private $previousExceptionClass;

/**
* @var
* @var string|null
*/
private $previousExceptionTrace;

/**
* @var
* @var string|null
*/
private $previousExceptionLine;

/**
* @var
* @var string|null
*/
private $previousExceptionFile;

Expand Down