Skip to content

Commit

Permalink
Optimize error message in errorhandling if displayErrorDetails was no…
Browse files Browse the repository at this point in the history
…t set
  • Loading branch information
marcelthole committed Oct 17, 2019
1 parent 26020e9 commit e9fce44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Slim/Handlers/ErrorHandler.php
Expand Up @@ -283,7 +283,10 @@ protected function writeToErrorLog(): void
{
$renderer = $this->callableResolver->resolve($this->logErrorRenderer);
$error = $renderer($this->exception, $this->logErrorDetails);
$error .= "\nView in rendered output by enabling the \"displayErrorDetails\" setting.\n";
if (!$this->displayErrorDetails) {
$error .= "\nTips: To display error details in HTTP response ";
$error .= 'set "displayErrorDetails" to true in the ErrorHandler constructor.';
}
$this->logError($error);
}

Expand Down
41 changes: 36 additions & 5 deletions tests/Handlers/ErrorHandlerTest.php
Expand Up @@ -307,16 +307,47 @@ public function testWriteToErrorLog()
'callableResolver' => $this->getCallableResolver(),
'responseFactory' => $this->getResponseFactory(),
])
->setMethods(['writeToErrorLog', 'logError'])
->setMethods(['logError'])
->getMock();

$handler->expects(self::once())
->method('logError')
->willReturnCallback(static function (string $error) {
self::assertStringNotContainsString(
'set "displayErrorDetails" to true in the ErrorHandler constructor',
$error
);
});

$exception = new HttpNotFoundException($request);
$handler->__invoke($request, $exception, true, true, true);
}

$handler
->expects($this->once())
->method('writeToErrorLog');
public function testWriteToErrorLogShowTip()
{
$request = $this
->createServerRequest('/', 'GET')
->withHeader('Accept', 'application/json');

$handler->__invoke($request, $exception, true, true, true);
$handler = $this->getMockBuilder(ErrorHandler::class)
->setConstructorArgs([
'callableResolver' => $this->getCallableResolver(),
'responseFactory' => $this->getResponseFactory(),
])
->setMethods(['logError'])
->getMock();

$handler->expects(self::once())
->method('logError')
->willReturnCallback(static function (string $error) {
self::assertStringContainsString(
'set "displayErrorDetails" to true in the ErrorHandler constructor',
$error
);
});

$exception = new HttpNotFoundException($request);
$handler->__invoke($request, $exception, false, true, true);
}

public function testDefaultErrorRenderer()
Expand Down

1 comment on commit e9fce44

@24rtj
Copy link

@24rtj 24rtj commented on e9fce44 Oct 30, 2019

Choose a reason for hiding this comment

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

.

Please sign in to comment.