Skip to content

Commit

Permalink
Move default renderer check to getRenderer().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad authored and Mark S committed Dec 7, 2016
1 parent 40b39d1 commit f4f8100
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Error/Middleware/ErrorHandlerMiddleware.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Error\ExceptionRenderer;
use Cake\Log\Log;
use Exception;

Expand Down Expand Up @@ -51,22 +52,28 @@ class ErrorHandlerMiddleware
'trace' => false,
];

/**
* Exception render.
*
* @var \Cake\Error\ExceptionRenderer|string|null
*/
protected $exceptionRenderer;

/**
* Constructor
*
* @param string|callable|null $renderer The renderer or class name
* @param string|callable|null $exceptionRenderer The renderer or class name
* to use or a callable factory. If null, Configure::read('Error.exceptionRenderer')
* will be used.
* @param array $config Configuration options to use. If empty, `Configure::read('Error')`
* will be used.
*/
public function __construct($renderer = null, array $config = [])
public function __construct($exceptionRenderer = null, array $config = [])
{
if ($renderer === null) {
$renderer = Configure::read('Error.exceptionRenderer');
if ($exceptionRenderer) {
$this->exceptionRenderer = $exceptionRenderer;
}

$this->renderer = $renderer ?: 'Cake\Error\ExceptionRenderer';
$config = $config ?: Configure::read('Error');
$this->config($config);
}
Expand Down Expand Up @@ -125,15 +132,19 @@ public function handleException($exception, $request, $response)
*/
protected function getRenderer($exception)
{
if (is_string($this->renderer)) {
$class = App::className($this->renderer, 'Error');
if (!$this->exceptionRenderer) {
$this->exceptionRenderer = $this->config('exceptionRender') ?: ExceptionRenderer::class;
}

if (is_string($this->exceptionRenderer)) {
$class = App::className($this->exceptionRenderer, 'Error');
if (!$class) {
throw new Exception("The '{$this->renderer}' renderer class could not be found.");
throw new Exception("The '{$this->exceptionRenderer}' renderer class could not be found.");
}

return new $class($exception);
}
$factory = $this->renderer;
$factory = $this->exceptionRenderer;

return $factory($exception);
}
Expand Down

0 comments on commit f4f8100

Please sign in to comment.