Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x - Add setLogErrorRenderer() to ErrorHandler #2801

Merged
merged 8 commits into from Aug 18, 2019
19 changes: 17 additions & 2 deletions Slim/Handlers/ErrorHandler.php
Expand Up @@ -42,6 +42,11 @@ class ErrorHandler implements ErrorHandlerInterface
*/
protected $defaultErrorRenderer = HtmlErrorRenderer::class;

/**
* @var ErrorRendererInterface|string|callable
*/
protected $logErrorRenderer = PlainTextErrorRenderer::class;

/**
* @var array
*/
Expand Down Expand Up @@ -259,15 +264,25 @@ public function setDefaultErrorRenderer(string $contentType, $errorRenderer): vo
$this->defaultErrorRenderer = $errorRenderer;
}

/**
* Set the renderer for the error logger
*
* @param ErrorRendererInterface|string|callable $logErrorRenderer
*/
public function setLogErrorRenderer($logErrorRenderer): void
{
$this->logErrorRenderer = $logErrorRenderer;
}

/**
* Write to the error log if $logErrors has been set to true
*
* @return void
*/
protected function writeToErrorLog(): void
{
$renderer = new PlainTextErrorRenderer();
$error = $renderer->__invoke($this->exception, $this->logErrorDetails);
$renderer = $this->callableResolver->resolve($this->logErrorRenderer);
$error = $renderer($this->exception, $this->logErrorDetails);
$error .= "\nView in rendered output by enabling the \"displayErrorDetails\" setting.\n";
$this->logError($error);
}
Expand Down
31 changes: 30 additions & 1 deletion tests/Handlers/ErrorHandlerTest.php
Expand Up @@ -11,13 +11,17 @@

use Psr\Http\Message\ResponseInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use RuntimeException;
use Slim\Error\Renderers\HtmlErrorRenderer;
use Slim\Error\Renderers\JsonErrorRenderer;
use Slim\Error\Renderers\PlainTextErrorRenderer;
use Slim\Error\Renderers\XmlErrorRenderer;
use Slim\Exception\HttpMethodNotAllowedException;
use Slim\Exception\HttpNotFoundException;
use Slim\Handlers\ErrorHandler;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Tests\Mocks\MockCustomException;
use Slim\Tests\TestCase;

Expand Down Expand Up @@ -322,12 +326,37 @@ public function testDefaultErrorRenderer()
->withHeader('Accept', 'application/unknown');

$handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory());
$exception = new \RuntimeException();
$exception = new RuntimeException();

/** @var ResponseInterface $res */
$res = $handler->__invoke($request, $exception, true, true, true);

$this->assertTrue($res->hasHeader('Content-Type'));
$this->assertEquals('text/html', $res->getHeaderLine('Content-Type'));
}

public function testLogErrorRenderer()
{
$renderer = function () {
return '';
};

$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);
$callableResolverProphecy
->resolve('logErrorRenderer')
->willReturn($renderer)
->shouldBeCalledOnce();

$handler = new ErrorHandler($callableResolverProphecy->reveal(), $this->getResponseFactory());
$handler->setLogErrorRenderer('logErrorRenderer');

$exception = new RuntimeException();
$exceptionProperty = new ReflectionProperty($handler, 'exception');
$exceptionProperty->setAccessible(true);
$exceptionProperty->setValue($handler, $exception);

$writeToErrorLogMethod = new ReflectionMethod($handler, 'writeToErrorLog');
$writeToErrorLogMethod->setAccessible(true);
$writeToErrorLogMethod->invoke($handler);
}
}