Skip to content

Commit

Permalink
Optionally include previous exception in log message if present
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Oct 23, 2018
1 parent 6fa82ae commit 454f8d5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
37 changes: 29 additions & 8 deletions src/Error/Middleware/ErrorHandlerMiddleware.php
Expand Up @@ -208,9 +208,33 @@ protected function logException($request, $exception)
* @return string Error message
*/
protected function getMessage($request, $exception)
{
$message = $this->getMessageForException($exception);

$message .= "\nRequest URL: " . $request->getRequestTarget();
$referer = $request->getHeaderLine('Referer');
if ($referer) {
$message .= "\nReferer URL: " . $referer;
}
if ($this->getConfig('trace')) {
$message .= "\nStack Trace:\n" . $exception->getTraceAsString() . "\n\n";
}

return $message;
}

/**
* Generate the message for the exception
*
* @param \Exception $exception The exception to log a message for.
* @param bool $isPrevious False for original exception, true for previous
* @return string Error message
*/
protected function getMessageForException($exception, $isPrevious = false)
{
$message = sprintf(
'[%s] %s',
'%s[%s] %s',
$isPrevious ? "\nPrevious: " : '',
get_class($exception),
$exception->getMessage()
);
Expand All @@ -222,13 +246,10 @@ protected function getMessage($request, $exception)
$message .= "\nException Attributes: " . var_export($exception->getAttributes(), true);
}
}
$message .= "\nRequest URL: " . $request->getRequestTarget();
$referer = $request->getHeaderLine('Referer');
if ($referer) {
$message .= "\nReferer URL: " . $referer;
}
if ($this->getConfig('trace')) {
$message .= "\nStack Trace:\n" . $exception->getTraceAsString() . "\n\n";

$previous = $exception->getPrevious();
if ($previous) {
$message .= $this->getMessageForException($previous, true);
}

return $message;
Expand Down
38 changes: 37 additions & 1 deletion tests/TestCase/Error/Middleware/ErrorHandlerMiddlewareTest.php
Expand Up @@ -178,7 +178,10 @@ public function testHandleExceptionLogAndTrace()
$this->stringContains('[Cake\Http\Exception\NotFoundException] Kaboom!'),
$this->stringContains('ErrorHandlerMiddlewareTest->testHandleException'),
$this->stringContains('Request URL: /target/url'),
$this->stringContains('Referer URL: /other/path')
$this->stringContains('Referer URL: /other/path'),
$this->logicalNot(
$this->stringContains('Previous: ')
)
));

$request = ServerRequestFactory::fromGlobals([
Expand All @@ -196,6 +199,39 @@ public function testHandleExceptionLogAndTrace()
$this->assertContains('was not found', '' . $result->getBody());
}

/**
* Test rendering an error page logs errors with previous
*
* @return void
*/
public function testHandleExceptionLogAndTraceWithPrevious()
{
$this->logger->expects($this->at(0))
->method('log')
->with('error', $this->logicalAnd(
$this->stringContains('[Cake\Http\Exception\NotFoundException] Kaboom!'),
$this->stringContains('Previous: [Cake\Datasource\Exception\RecordNotFoundException] Previous logged'),
$this->stringContains('ErrorHandlerMiddlewareTest->testHandleException'),
$this->stringContains('Request URL: /target/url'),
$this->stringContains('Referer URL: /other/path')
));

$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/target/url',
'HTTP_REFERER' => '/other/path'
]);
$response = new Response();
$middleware = new ErrorHandlerMiddleware(null, ['log' => true, 'trace' => true]);
$next = function ($req, $res) {
$previous = new \Cake\Datasource\Exception\RecordNotFoundException('Previous logged');
throw new \Cake\Http\Exception\NotFoundException('Kaboom!', null, $previous);
};
$result = $middleware($request, $response, $next);
$this->assertNotSame($result, $response);
$this->assertEquals(404, $result->getStatusCode());
$this->assertContains('was not found', '' . $result->getBody());
}

/**
* Test rendering an error page skips logging for specific classes
*
Expand Down

0 comments on commit 454f8d5

Please sign in to comment.