diff --git a/phpstan.neon b/phpstan.neon index 3f8ce6e..99c4d7f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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/ diff --git a/src/EventListener/MessengerProfilerListener.php b/src/EventListener/MessengerProfilerListener.php index 8c35dc6..8b91e32 100644 --- a/src/EventListener/MessengerProfilerListener.php +++ b/src/EventListener/MessengerProfilerListener.php @@ -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 { @@ -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); } diff --git a/src/Messenger/ProfilerMiddleware.php b/src/Messenger/ProfilerMiddleware.php index 13a368a..3bd1be0 100755 --- a/src/Messenger/ProfilerMiddleware.php +++ b/src/Messenger/ProfilerMiddleware.php @@ -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; @@ -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);