From bb96d393c1270fe8e8aa68d248db61e7911dd9e1 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Thu, 9 May 2024 20:41:20 +0100 Subject: [PATCH] Only render tip to error log if plain text renderer is used 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 --- Slim/Handlers/ErrorHandler.php | 2 +- tests/Handlers/ErrorHandlerTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Slim/Handlers/ErrorHandler.php b/Slim/Handlers/ErrorHandler.php index f9606e364..34c1eabf3 100644 --- a/Slim/Handlers/ErrorHandler.php +++ b/Slim/Handlers/ErrorHandler.php @@ -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.'; } diff --git a/tests/Handlers/ErrorHandlerTest.php b/tests/Handlers/ErrorHandlerTest.php index 9346f1c7e..b50dff4b3 100644 --- a/tests/Handlers/ErrorHandlerTest.php +++ b/tests/Handlers/ErrorHandlerTest.php @@ -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