Skip to content

Commit

Permalink
fix: use WrappedExceptionsInterface::getWrappedExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbrault committed Apr 9, 2024
1 parent 69be5eb commit 0b5dd53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ parameters:
reportUnmatched: false
- message: '#Call to deprecated method getMasterRequest\(\) of class Symfony\\Component\\HttpFoundation\\RequestStack#'
reportUnmatched: false
- message: '#Call to deprecated method getNestedExceptions.+#'
reportUnmatched: false
- message: '#Call to function method_exists\(\) with Symfony\\Component\\Messenger\\Exception\\HandlerFailedException and .getNestedExceptions. will always evaluate to true.#'
reportUnmatched: false

scanDirectories:
- vendor/datadog/dd-trace/src/DDTrace/
Expand Down
23 changes: 13 additions & 10 deletions src/EventListener/MessengerProfilerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Event\WorkerStartedEvent;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\WrappedExceptionsInterface;

class MessengerProfilerListener implements EventSubscriberInterface
{
Expand Down Expand Up @@ -48,19 +49,21 @@ public function onReject(WorkerMessageFailedEvent $event): void
{
$throwable = $event->getThrowable();

if ($throwable instanceof HandlerFailedException) {
$nestedExceptions = [];
$nestedExceptions = [];

if (method_exists($throwable, 'getNestedExceptions')) {
$nestedExceptions = $throwable->getNestedExceptions();
} elseif (method_exists($throwable, 'getWrappedExceptions')) {
$nestedExceptions = $throwable->getWrappedExceptions();
}
if (interface_exists(WrappedExceptionsInterface::class)
&& $throwable instanceof WrappedExceptionsInterface
) {
$nestedExceptions = $throwable->getWrappedExceptions();
} elseif ($throwable instanceof HandlerFailedException
&& method_exists($throwable, 'getNestedExceptions')
) {
$nestedExceptions = $throwable->getNestedExceptions();
}

$firstNestedException = reset($nestedExceptions);
$firstNestedException = reset($nestedExceptions);

$throwable = false !== $firstNestedException ? $firstNestedException : $throwable;
}
$throwable = false !== $firstNestedException ? $firstNestedException : $throwable;

$this->profiler->stop($throwable);
}
Expand Down
16 changes: 14 additions & 2 deletions src/Messenger/ProfilerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\WrappedExceptionsInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
Expand Down Expand Up @@ -62,9 +63,20 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
return $stack->next()
->handle($envelope, $stack)
;
} catch (HandlerFailedException $exception) {
if ($shouldStop) {
} catch (\Exception $exception) {
$nestedExceptions = null;

if (interface_exists(WrappedExceptionsInterface::class)
&& $exception instanceof WrappedExceptionsInterface
) {
$nestedExceptions = $exception->getWrappedExceptions();
} elseif ($exception instanceof HandlerFailedException
&& method_exists($exception, 'getNestedExceptions')
) {
$nestedExceptions = $exception->getNestedExceptions();
}

if ($shouldStop && null !== $nestedExceptions) {
$firstNestedException = reset($nestedExceptions);

$this->profiler->stop(false !== $firstNestedException ? $firstNestedException : $exception);
Expand Down

0 comments on commit 0b5dd53

Please sign in to comment.