Skip to content

[Messenger] Fix TraceableMessageBus implementation so it can compute caller even when used within a callback #43781

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,22 @@ public function testItTracesExceptions()
],
], $actualTracedMessage);
}

public function testItTracesExceptionsWhenMessageBusIsFiredFromArrayCallback()
{
$message = new DummyMessage('Hello');
$exception = new \RuntimeException();

$bus = $this->createMock(MessageBusInterface::class);
$bus->expects($this->once())
->method('dispatch')
->with($message)
->willThrowException($exception);

$traceableBus = new TraceableMessageBus($bus);

$this->expectExceptionObject($exception);

array_map([$traceableBus, 'dispatch'], [$message]);
}
}
13 changes: 8 additions & 5 deletions src/Symfony/Component/Messenger/TraceableMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ private function getCaller(): array
{
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 8);

$file = $trace[1]['file'];
$line = $trace[1]['line'];
$file = $trace[1]['file'] ?? null;
$line = $trace[1]['line'] ?? null;

$handleTraitFile = (new \ReflectionClass(HandleTrait::class))->getFileName();
$found = false;
Expand Down Expand Up @@ -97,9 +97,12 @@ private function getCaller(): array
}
}

$name = str_replace('\\', '/', $file);
$name = substr($name, strrpos($name, '/') + 1);
$name = str_replace('\\', '/', (string) $file);

return compact('name', 'file', 'line');
return [
'name' => substr($name, strrpos($name, '/') + 1),
'file' => $file,
'line' => $line,
];
}
}