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

add previous exception messages to plain text output #609

Merged
merged 2 commits into from Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this switch? Do you know a case where we dont want the previous exceptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe someone don't want it in the logs. or they show it to the client and want to hide it there (for example the message could contain internal information that should only be visible to developers).


/**
* @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 .= "\nBefore: " . $this->getExceptionOutput($previous);
tflori marked this conversation as resolved.
Show resolved Hide resolved
$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
*/
protected function getExceptionOutput($exception)
tflori marked this conversation as resolved.
Show resolved Hide resolved
{
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,
'Before: ' . 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