Skip to content

Commit

Permalink
Merge pull request #609 from tflori/plaintext-previous
Browse files Browse the repository at this point in the history
add previous exception messages to plain text output
  • Loading branch information
denis-sokolov committed Jun 21, 2019
2 parents bb6f11f + 54bf2df commit 96b5407
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 18 deletions.
56 changes: 48 additions & 8 deletions src/Whoops/Handler/PlainTextHandler.php
Expand Up @@ -46,6 +46,11 @@ class PlainTextHandler extends Handler
*/
private $traceFunctionArgsOutputLimit = 1024;

/**
* @var bool
*/
private $addPreviousToOutput = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -114,6 +119,21 @@ public function addTraceToOutput($addTraceToOutput = null)
return $this;
}

/**
* Add previous exceptions to output.
* @param bool|null $addPreviousToOutput
* @return bool|$this
*/
public function addPreviousToOutput($addPreviousToOutput = null)
{
if (func_num_args() == 0) {
return $this->addPreviousToOutput;
}

$this->addPreviousToOutput = (bool) $addPreviousToOutput;
return $this;
}

/**
* Add error trace function arguments to output.
* Set to True for all frame args, or integer for the n first frame args.
Expand Down Expand Up @@ -151,14 +171,18 @@ public function setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit)
public function generateResponse()
{
$exception = $this->getException();
return sprintf(
"%s: %s in file %s on line %d%s\n",
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$this->getTraceOutput()
);
$message = $this->getExceptionOutput($exception);

if ($this->addPreviousToOutput) {
$previous = $exception->getPrevious();
while ($previous) {
$message .= "\n\nCaused by\n" . $this->getExceptionOutput($previous);
$previous = $previous->getPrevious();
}
}


return $message . $this->getTraceOutput() . "\n";
}

/**
Expand Down Expand Up @@ -284,6 +308,22 @@ private function getTraceOutput()
return $response;
}

/**
* Get the exception as plain text.
* @param \Throwable $exception
* @return string
*/
private function getExceptionOutput($exception)
{
return sprintf(
"%s: %s in file %s on line %d",
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
);
}

/**
* @return int
*/
Expand Down
81 changes: 71 additions & 10 deletions tests/Whoops/Handler/PlainTextHandlerTest.php
Expand Up @@ -13,6 +13,9 @@

class PlainTextHandlerTest extends TestCase
{
const DEFAULT_EXCEPTION_LINE = 34;
const DEFAULT_LINE_OF_CALLER = 65;

/**
* @throws \InvalidArgumentException If argument is not null or a LoggerInterface
* @param \Psr\Log\LoggerInterface|null $logger
Expand All @@ -32,28 +35,34 @@ public function getException($message = 'test message')
}

/**
* @param bool $withTrace
* @param bool $withTraceArgs
* @param bool $loggerOnly
* @param bool $withTrace
* @param bool $withTraceArgs
* @param int $traceFunctionArgsOutputLimit
* @param bool $loggerOnly
* @param bool $previousOutput
* @param null $exception
* @return array
*/
private function getPlainTextFromHandler(
$withTrace = false,
$withTraceArgs = false,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false
$loggerOnly = false,
$previousOutput = false,
$exception = null
) {
$handler = $this->getHandler();
$handler->addTraceToOutput($withTrace);
$handler->addTraceFunctionArgsToOutput($withTraceArgs);
$handler->setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit);
$handler->addPreviousToOutput($previousOutput);
$handler->loggerOnly($loggerOnly);

$run = $this->getRunInstance();
$run->pushHandler($handler);
$run->register();

$exception = $this->getException();
$exception = $exception ?: $this->getException();
ob_start();
$run->handleException($exception);

Expand Down Expand Up @@ -209,7 +218,59 @@ public function testReturnsWithoutFramesOutput()
get_class($this->getException()),
'test message',
__FILE__,
31
self::DEFAULT_EXCEPTION_LINE
),
$text
);
}

public function testReturnsWithoutPreviousExceptions()
{
$text = $this->getPlainTextFromHandler(
$withTrace = false,
$withTraceArgs = true,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false,
$previousOutput = false,
new RuntimeException('Outer exception message', 0, new RuntimeException('Inner exception message'))
);

// Check that the response does not contain Inner exception message:
$this->assertNotContains(
sprintf(
"%s: %s in file %s",
RuntimeException::class,
'Inner exception message',
__FILE__
),
$text
);
}

public function testReturnsWithPreviousExceptions()
{
$text = $this->getPlainTextFromHandler(
$withTrace = false,
$withTraceArgs = true,
$traceFunctionArgsOutputLimit = 1024,
$loggerOnly = false,
$previousOutput = true,
new RuntimeException('Outer exception message', 0, new RuntimeException('Inner exception message'))
);

// Check that the response has the correct message:
$this->assertEquals(
sprintf(
"%s: %s in file %s on line %d\n" .
"%s: %s in file %s on line %d\n",
RuntimeException::class,
'Outer exception message',
__FILE__,
258,
"\nCaused by\n" . RuntimeException::class,
'Inner exception message',
__FILE__,
258
),
$text
);
Expand Down Expand Up @@ -238,10 +299,10 @@ public function testReturnsWithFramesOutput()
sprintf(
'%3d. %s->%s() %s:%d',
2,
'Whoops\Handler\PlainTextHandlerTest',
__CLASS__,
'getException',
__FILE__,
56
self::DEFAULT_LINE_OF_CALLER
),
$text
);
Expand Down Expand Up @@ -280,7 +341,7 @@ public function testReturnsWithFramesAndArgsOutput()
'Whoops\Handler\PlainTextHandlerTest',
'getException',
__FILE__,
56
self::DEFAULT_LINE_OF_CALLER
),
$text
);
Expand Down Expand Up @@ -322,7 +383,7 @@ public function testReturnsWithFramesAndLimitedArgsOutput()
'Whoops\Handler\PlainTextHandlerTest',
'getException',
__FILE__,
56
self::DEFAULT_LINE_OF_CALLER
),
$text
);
Expand Down

0 comments on commit 96b5407

Please sign in to comment.