Skip to content

Commit 23c4d7d

Browse files
committed
capture root exception when messenger fails
1 parent 12f9d17 commit 23c4d7d

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/Listeners/ConsoleEventsSubscriber.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ public static function getSubscribedEvents(): array
6363
*/
6464
public function onConsoleStart(ConsoleCommandEvent $event): void
6565
{
66-
$command = $event->getCommand();
66+
$commandName = $event->getCommand()?->getName();
6767

68-
if (null === $command || $this->isIgnored($command)) {
68+
if (null === $commandName || $this->isIgnored($commandName)) {
6969
return;
7070
}
7171

72-
$commandName = $command->getName();
73-
7472
if ($this->inspector->needTransaction()) {
7573
$this->inspector->startTransaction($commandName)
7674
->setType('command')
@@ -90,9 +88,9 @@ public function onConsoleStart(ConsoleCommandEvent $event): void
9088
*/
9189
public function onConsoleError(ConsoleErrorEvent $event): void
9290
{
93-
$command = $event->getCommand();
91+
$commandName = $event->getCommand()?->getName();
9492

95-
if (null === $command || $this->isIgnored($command) || ! $this->inspector->isRecording()) {
93+
if (null === $commandName || $this->isIgnored($commandName) || ! $this->inspector->isRecording()) {
9694
return;
9795
}
9896

@@ -101,14 +99,12 @@ public function onConsoleError(ConsoleErrorEvent $event): void
10199

102100
public function onConsoleTerminate(ConsoleTerminateEvent $event): void
103101
{
104-
$command = $event->getCommand();
102+
$commandName = $event->getCommand()?->getName();
105103

106-
if (null === $command || $this->isIgnored($command)) {
104+
if (null === $commandName || $this->isIgnored($commandName)) {
107105
return;
108106
}
109107

110-
$commandName = $command->getName();
111-
112108
if($this->inspector->hasTransaction() && $this->inspector->transaction()->name === $commandName) {
113109
$this->inspector->transaction()->setResult($event->getExitCode() === 0 ? 'success' : 'error');
114110
} elseif(\array_key_exists($commandName, $this->segments)) {
@@ -122,9 +118,9 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event): void
122118

123119
public function onConsoleSignal(ConsoleSignalEvent $event): void
124120
{
125-
$command = $event->getCommand();
121+
$commandName = $event->getCommand()?->getName();
126122

127-
if (null === $command || $this->isIgnored($command)) {
123+
if (null === $commandName || $this->isIgnored($commandName)) {
128124
return;
129125
}
130126

@@ -133,10 +129,10 @@ public function onConsoleSignal(ConsoleSignalEvent $event): void
133129
}
134130
}
135131

136-
protected function isIgnored(Command $command): bool
132+
protected function isIgnored(string $command): bool
137133
{
138134
foreach ($this->ignoredCommands as $pattern) {
139-
if (Filters::matchWithWildcard($pattern, $command->getName())) {
135+
if (Filters::matchWithWildcard($pattern, $command)) {
140136
return true;
141137
}
142138
}

src/Messenger/MessengerMonitoringMiddleware.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Inspector\Models\Segment;
77
use Inspector\Symfony\Bundle\Filters;
88
use Symfony\Component\Messenger\Envelope;
9+
use Symfony\Component\Messenger\Exception\DelayedMessageHandlingException;
10+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
11+
use Symfony\Component\Messenger\Exception\WrappedExceptionsInterface;
912
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1013
use Symfony\Component\Messenger\Middleware\StackInterface;
1114
use Symfony\Component\Messenger\Stamp\HandledStamp;
@@ -36,13 +39,13 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
3639
}
3740

3841
try {
39-
// Before handling the message in sync mode
42+
// Before handling the message
4043
$this->beforeHandle($class);
4144

4245
// Handle the message
4346
$envelope = $stack->next()->handle($envelope, $stack);
4447

45-
// After handling the message in sync mode
48+
// After handling the message
4649
$this->afterHandle($envelope->all(HandledStamp::class));
4750

4851
return $envelope;
@@ -88,9 +91,25 @@ protected function afterHandle(array $handlers): void
8891
}
8992
}
9093

91-
protected function errorHandle(\Throwable $error): void
94+
protected function errorHandle(\Throwable $exception): void
9295
{
93-
$this->inspector->reportException($error, false);
96+
if ($exception instanceof WrappedExceptionsInterface) {
97+
$exception = $exception->getWrappedExceptions();
98+
} elseif ($exception instanceof HandlerFailedException && \method_exists($exception, 'getNestedExceptions')) {
99+
$exception = $exception->getNestedExceptions();
100+
} elseif ($exception instanceof DelayedMessageHandlingException && \method_exists($exception, 'getExceptions')) {
101+
$exception = $exception->getExceptions();
102+
}
103+
104+
if (\is_array($exception)) {
105+
foreach ($exception as $nestedException) {
106+
$this->errorHandle($nestedException);
107+
}
108+
109+
return;
110+
}
111+
112+
$this->inspector->reportException($exception, false);
94113
$this->inspector->transaction()->setResult('error');
95114
}
96115

0 commit comments

Comments
 (0)