From e9fce44a56f80ca5834bda64e39a525476314770 Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Thu, 17 Oct 2019 19:49:52 +0200 Subject: [PATCH] Optimize error message in errorhandling if displayErrorDetails was not set --- Slim/Handlers/ErrorHandler.php | 5 +++- tests/Handlers/ErrorHandlerTest.php | 41 +++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Slim/Handlers/ErrorHandler.php b/Slim/Handlers/ErrorHandler.php index 8ac1755df..1d00defdd 100644 --- a/Slim/Handlers/ErrorHandler.php +++ b/Slim/Handlers/ErrorHandler.php @@ -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); } diff --git a/tests/Handlers/ErrorHandlerTest.php b/tests/Handlers/ErrorHandlerTest.php index 8993cc3ab..3c5d8392a 100644 --- a/tests/Handlers/ErrorHandlerTest.php +++ b/tests/Handlers/ErrorHandlerTest.php @@ -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()