-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Move default into class so simpler ::class syntax can be used. #9806
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -51,17 +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 | ||
* to use or a callable factory. | ||
* @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 = []) | ||
{ | ||
$this->renderer = $renderer ?: 'Cake\Error\ExceptionRenderer'; | ||
if ($exceptionRenderer) { | ||
$this->exceptionRenderer = $exceptionRenderer; | ||
} | ||
|
||
$config = $config ?: Configure::read('Error'); | ||
$this->config($config); | ||
} | ||
|
@@ -120,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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason this couldn't be done in the constructor? Are you trying to avoid the class load? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the need to check for default / fallback class so early until it needs to be actually instantiated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a case for using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, but that would introduce strings again, which is what I think @dereuromark is trying to remove. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I only wanted to not have it outside to be a requirement to be passed in :) See the linked app PR |
||
|
||
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); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still needs to be
exceptionRenderer
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the config value doesn't!atch the one in the app skeleton