-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Optionally handle subclasses of exceptions in custom error handler #2862
Conversation
Force pushed for typo. |
We could mitigate the BC by doing this instead:
Then even if a someone has subclassed ErrorMiddleware and overriden $handlers to contain callables while not overriding the |
@adriansuter I think this would be a good addition and there's no BC breaks. What do you think about it? |
Is there a reason that |
In case one wants to extend |
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.
That is a good idea.
Another solution would be to add a new protected property subClassHandlers
and then just write
public function setErrorHandler(string $type, $handler, bool $handleSubclasses = false): self
{
if ($handleSubclasses) {
$this->subClassHandlers[$type] = $handler;
} else {
$this->handlers[$type] = $handler;
}
return $this;
}
That way, we do not change the data logic of the handlers
property which is expected to be a plain array of handlers.
Of course we would have to change getErrorHandler()
too.
public function getErrorHandler(string $type)
{
if (isset($this->handlers[$type])) {
return $this->callableResolver->resolve($this->handlers[$type]);
} elseif (isset($this->subClassHandlers[$type])) {
return $this->callableResolver->resolve($this->subClassHandlers[$type]);
} else {
foreach ($this->subClassHandlers as $class => $handler) {
if (is_subclass_of($type, $class)) {
return $this->callableResolver->resolve($handler);
}
}
}
return $this->getDefaultErrorHandler();
}
That way, we would also get rid of the double if
-statements in the foreach
loop.
What do you mean, @l0gicgate and @nickdnk ? If you want to leave it as requested in this PR, then I am fine too.
I think yours is a good workaround, @adriansuter. |
@adriansuter I like your solution. We should proceed with that. |
Updated the PR. Also, I added phpdocs for the handler arrays based on the value of |
Need to write tests as well for the subclass case. Are you able to do that? |
I did. I don't know why it claims it wasn't covered. There is a test for both with and without subclass use. |
Did you add a test case for the case you set an error handler as subclass handler, but the exception thrown is not a subclass but exactly that class?
|
I think that did it. |
…ndlers Implemented subclass handler array to avoid breaking changes Cleaned up test syntax a bit
Hello guys
I thought this could be a nice addition, so a particular app can decide to handle all subclasses of an exception with one handler. This is nice way to keep things separated.
I added this as an option that defaults to false so this should not be a breaking change, but I'm not 100% sure since
$handlers
isprotected
and notprivate
.What do you think?