-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Add method HandlerFailedException::getNestedExceptionOfClass #34704
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
Conversation
ef17d8c
to
cdbe8c8
Compare
src/Symfony/Component/Messenger/Tests/Fixtures/MyOwnException.php
Outdated
Show resolved
Hide resolved
cdbe8c8
to
18981b6
Compare
|
||
public function hasNestedExceptionOfClass(string $exceptionClassName): bool | ||
{ | ||
return 0 < \count($this->getNestedExceptionOfClass($exceptionClassName)); |
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.
I'm not sure about this method: it looks like calling for double computation of the logic.
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.
Hi ! You mean, I should remove it or you have an idea to improve it ?
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.
If we have 20 exceptions, and the first one is $exceptionClassName
, we will still count the other 19. Right?
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.
can you please remove this method?
return array_filter( | ||
$this->exceptions, | ||
function ($exception) use ($exceptionClassName) { | ||
return \get_class($exception) === $exceptionClassName; |
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.
should this be an exact match, or an is_a()
check?
you feature request looks like an instanceof
check. This would mean is_a()
here.
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.
Ok good catch. In my mind it was a is_a
check but I should admin an instanceof
looks better indeed.
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.
Lets use is_a()
instead.
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.
please add some related test cases also.
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.
Yes please! I can see this is somewhat useful.
I often write code like:
try {
$this->bus->dispatch($command);
} catch (HandlerFailedException $e) {
$exception = $e->getPrevious();
if ($exception instanceof FooException) {
// Handle it one way
} else {
// I dont know what to do...
throw $e;
}
}
Now I can write:
try {
$this->bus->dispatch($command);
} catch (HandlerFailedException $e) {
if ($e->hasNestedExceptionOfClass(FooException::class)) {
// Handle it one way
} else {
// I dont know what to do...
throw $e;
}
}
|
||
public function hasNestedExceptionOfClass(string $exceptionClassName): bool | ||
{ | ||
return 0 < \count($this->getNestedExceptionOfClass($exceptionClassName)); |
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.
If we have 20 exceptions, and the first one is $exceptionClassName
, we will still count the other 19. Right?
return array_filter( | ||
$this->exceptions, | ||
function ($exception) use ($exceptionClassName) { | ||
return \get_class($exception) === $exceptionClassName; |
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.
Lets use is_a()
instead.
Hello @Nyholm ! Thanks for the reminder. Glad to see I'm not alone writing this kind of code ;) I postponed updating this PR for a long time. I will fix that asap. |
When dispatching a new command from a command handler it could cause additional nesting of
Is this something we would like to consider? |
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.
Friendly ping @tyx
|
||
public function hasNestedExceptionOfClass(string $exceptionClassName): bool | ||
{ | ||
return 0 < \count($this->getNestedExceptionOfClass($exceptionClassName)); |
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.
can you please remove this method?
return array_filter( | ||
$this->exceptions, | ||
function ($exception) use ($exceptionClassName) { | ||
return \get_class($exception) === $exceptionClassName; |
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.
please add some related test cases also.
A nice little ping again =) |
18981b6
to
89083e3
Compare
Nice timing @Nyholm ;) I just updated and rebased the PR |
Thank you for the update. I think the current code looks good. Im just curious about something. Im not sure we should change anything, but I just want to ask. Is there someway we can stop dealing with arrays? Maybe Im just interested in the first error. Ie: try {
$this->bus->dispatch($command);
} catch (HandlerFailedException $e) {
if (null !== $exception = $e->getFirstNestedExceptionOfClass(FooException::class)) {
echo $exception->getMessage()
} else {
throw $e;
}
} The vast majority of the times, I get an HandlerFailedExcpetion with just a single exception in it. Do we care? |
89083e3
to
99d8371
Compare
To know if specific exception are the origin of messenger failure
99d8371
to
e0dd84b
Compare
Regarding the PR I made a small tweak to ensure returning an array with correct indexes (and update test). For your point @Nyholm you're totally right. I do the same ! May be we could also add another method to get only the first occurence as sugar ? |
Thank you @tyx. |
I have many times use this kind of code on my own development. It helps when dealing with specific exception through a Messenger usage.
Example in an ExceptionListener, the exception you get could be a
HandlerFailedException
but you want a specific treatment when the original exception is different (like a RedirectResponse, or Http error code different).edit: I finally also added a getNestedExceptionOfClass that could be useful too