Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ad1add

Browse files
committed
[Messenger] Improve formatting of thrown exception in show failed message command
1 parent d658164 commit 2ad1add

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/Symfony/Component/Messenger/Command/AbstractFailedMessagesCommand.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Helper\Dumper;
1616
use Symfony\Component\Console\Style\SymfonyStyle;
17+
use Symfony\Component\ErrorHandler\Exception\FlattenException;
1718
use Symfony\Component\Messenger\Envelope;
1819
use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
1920
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
2021
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
2122
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
2223
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
2324
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
25+
use Symfony\Component\VarDumper\Caster\Caster;
26+
use Symfony\Component\VarDumper\Caster\TraceStub;
27+
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
28+
use Symfony\Component\VarDumper\Cloner\Stub;
29+
use Symfony\Component\VarDumper\Cloner\VarCloner;
2430

2531
/**
2632
* @author Ryan Weaver <[email protected]>
@@ -121,7 +127,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io)
121127

122128
if ($io->isVeryVerbose()) {
123129
$io->title('Message:');
124-
$dump = new Dumper($io);
130+
$dump = new Dumper($io, null, $this->createCloner());
125131
$io->writeln($dump($envelope->getMessage()));
126132
$io->title('Exception:');
127133
$flattenException = null;
@@ -130,7 +136,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io)
130136
} elseif (null !== $lastRedeliveryStampWithException) {
131137
$flattenException = $lastRedeliveryStampWithException->getFlattenException();
132138
}
133-
$io->writeln(null === $flattenException ? '(no data)' : $flattenException->getTraceAsString());
139+
$io->writeln(null === $flattenException ? '(no data)' : $dump($flattenException));
134140
} else {
135141
$io->writeln(' Re-run command with <info>-vv</info> to see more message & error details.');
136142
}
@@ -172,4 +178,26 @@ protected function getLastRedeliveryStampWithException(Envelope $envelope): ?Red
172178

173179
return null;
174180
}
181+
182+
private function createCloner(): ?ClonerInterface
183+
{
184+
if (!class_exists(VarCloner::class)) {
185+
return null;
186+
}
187+
188+
$cloner = new VarCloner();
189+
$cloner->addCasters([FlattenException::class => function (FlattenException $flattenException, array $a, Stub $stub): array {
190+
$stub->class = $flattenException->getClass();
191+
192+
return [
193+
Caster::PREFIX_VIRTUAL.'message' => $flattenException->getMessage(),
194+
Caster::PREFIX_VIRTUAL.'code' => $flattenException->getCode(),
195+
Caster::PREFIX_VIRTUAL.'file' => $flattenException->getFile(),
196+
Caster::PREFIX_VIRTUAL.'line' => $flattenException->getLine(),
197+
Caster::PREFIX_VIRTUAL.'trace' => new TraceStub($flattenException->getTrace()),
198+
];
199+
}]);
200+
201+
return $cloner;
202+
}
175203
}

src/Symfony/Component/Messenger/Tests/Command/FailedMessagesShowCommandTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Output\OutputInterface;
1516
use Symfony\Component\Console\Tester\CommandTester;
1617
use Symfony\Component\Messenger\Command\FailedMessagesShowCommand;
1718
use Symfony\Component\Messenger\Envelope;
@@ -234,4 +235,44 @@ public function testInvalidMessagesThrowsException()
234235
$tester = new CommandTester($command);
235236
$tester->execute(['id' => 15]);
236237
}
238+
239+
public function testVeryVerboseOutputForSingleMessageContainsExceptionWithTrace()
240+
{
241+
$exception = new \RuntimeException('Things are bad!');
242+
$exceptionLine = __LINE__ - 1;
243+
$envelope = new Envelope(new \stdClass(), [
244+
new TransportMessageIdStamp(15),
245+
new SentToFailureTransportStamp('async'),
246+
new RedeliveryStamp(0),
247+
new ErrorDetailsStamp($exception),
248+
]);
249+
$receiver = $this->createMock(ListableReceiverInterface::class);
250+
$receiver->expects($this->once())->method('find')->with(42)->willReturn($envelope);
251+
252+
$command = new FailedMessagesShowCommand('failure_receiver', $receiver);
253+
$tester = new CommandTester($command);
254+
$tester->execute(['id' => 42], ['verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE]);
255+
$this->assertStringMatchesFormat(sprintf(<<<'EOF'
256+
%%A
257+
Exception:
258+
==========
259+
260+
RuntimeException {
261+
message: "Things are bad!"
262+
code: 0
263+
file: "%s"
264+
line: %d
265+
trace: {
266+
%%s%%eTests%%eCommand%%eFailedMessagesShowCommandTest.php:%d {
267+
Symfony\Component\Messenger\Tests\Command\FailedMessagesShowCommandTest->testVeryVerboseOutputForSingleMessageContainsExceptionWithTrace()
268+
› {
269+
› $exception = new \RuntimeException('Things are bad!');
270+
› $exceptionLine = __LINE__ - 1;
271+
}
272+
%%A
273+
EOF
274+
,
275+
__FILE__, $exceptionLine, $exceptionLine),
276+
$tester->getDisplay(true));
277+
}
237278
}

0 commit comments

Comments
 (0)