Skip to content

Commit

Permalink
Only render tip to error log if plain text renderer is used
Browse files Browse the repository at this point in the history
If the logErrorRenderer is not the plain text renderer then we must not
render the tips message as this will break the structured format of the
message that's been rendered.

Closes #3317
  • Loading branch information
akrabat committed May 9, 2024
1 parent ad92887 commit bb96d39
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Slim/Handlers/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected function writeToErrorLog(): void
{
$renderer = $this->callableResolver->resolve($this->logErrorRenderer);
$error = $renderer($this->exception, $this->logErrorDetails);
if (!$this->displayErrorDetails) {
if ($this->logErrorRenderer === PlainTextErrorRenderer::class && !$this->displayErrorDetails) {
$error .= "\nTips: To display error details in HTTP response ";
$error .= 'set "displayErrorDetails" to true in the ErrorHandler constructor.';
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Handlers/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,35 @@ public function testWriteToErrorLogShowTip()
$handler->__invoke($request, $exception, false, true, true);
}

public function testWriteToErrorLogDoesNotShowTipIfErrorLogRendererIsNotPlainText()
{
$request = $this
->createServerRequest('/', 'GET')
->withHeader('Accept', 'application/json');

$logger = $this->getMockLogger();

$handler = new ErrorHandler(
$this->getCallableResolver(),
$this->getResponseFactory(),
$logger
);

$handler->setLogErrorRenderer(HtmlErrorRenderer::class);

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

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

public function testDefaultErrorRenderer()
{
$request = $this
Expand Down

0 comments on commit bb96d39

Please sign in to comment.