Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd2bac32

Browse files
committed
feature#38940 [Messenger] Improve formatting of exception in failed message (Jeroen Noten)
This PR was merged into the 5.2-dev branch.Discussion----------[Messenger] Improve formatting of exception in failed message| Q | A| ------------- | ---| Branch? | 5.x| Bug fix? | no| New feature? | not really, enhancement of an existing feature| Deprecations? | no| Tickets |Fix#32310| License | MITThis PR improves the formatting of exception details in failed messenges when displayed using `messenger:failed:show <id> -vv`.Before:<img width="807" alt="Screen Shot 2020-11-01 at 1 05 24 PM" src="https://user-images.githubusercontent.com/4370753/97802602-ea593200-1c44-11eb-8bcb-7fcf2d3f1db0.png">After:<img width="803" alt="Screen Shot 2020-11-01 at 1 03 09 PM" src="https://user-images.githubusercontent.com/4370753/97802615-f0e7a980-1c44-11eb-8c12-46b2d4510364.png">I created a `ThrownExceptionDetails` class which will be displayed as a normal exception when dumped with the VarDumper component. Not sure if this is the right way to do it and if the class is in the right namespace, but this is the best solution I could came up with tofix#32310. I'm open for other suggestions.Commits-------2ad1add [Messenger] Improve formatting of thrown exception in show failed message command
2 parents6251c4e +2ad1add commitd2bac32

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
useSymfony\Component\Console\Command\Command;
1515
useSymfony\Component\Console\Helper\Dumper;
1616
useSymfony\Component\Console\Style\SymfonyStyle;
17+
useSymfony\Component\ErrorHandler\Exception\FlattenException;
1718
useSymfony\Component\Messenger\Envelope;
1819
useSymfony\Component\Messenger\Stamp\ErrorDetailsStamp;
1920
useSymfony\Component\Messenger\Stamp\RedeliveryStamp;
2021
useSymfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
2122
useSymfony\Component\Messenger\Stamp\TransportMessageIdStamp;
2223
useSymfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
2324
useSymfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
25+
useSymfony\Component\VarDumper\Caster\Caster;
26+
useSymfony\Component\VarDumper\Caster\TraceStub;
27+
useSymfony\Component\VarDumper\Cloner\ClonerInterface;
28+
useSymfony\Component\VarDumper\Cloner\Stub;
29+
useSymfony\Component\VarDumper\Cloner\VarCloner;
2430

2531
/**
2632
* @author Ryan Weaver <ryan@symfonycasts.com>
@@ -121,7 +127,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io)
121127

122128
if ($io->isVeryVerbose()) {
123129
$io->title('Message:');
124-
$dump =newDumper($io);
130+
$dump =newDumper($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
returnnull;
174180
}
181+
182+
privatefunctioncreateCloner(): ?ClonerInterface
183+
{
184+
if (!class_exists(VarCloner::class)) {
185+
returnnull;
186+
}
187+
188+
$cloner =newVarCloner();
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' =>newTraceStub($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
namespaceSymfony\Component\Messenger\Tests\Command;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Console\Output\OutputInterface;
1516
useSymfony\Component\Console\Tester\CommandTester;
1617
useSymfony\Component\Messenger\Command\FailedMessagesShowCommand;
1718
useSymfony\Component\Messenger\Envelope;
@@ -234,4 +235,44 @@ public function testInvalidMessagesThrowsException()
234235
$tester =newCommandTester($command);
235236
$tester->execute(['id' =>15]);
236237
}
238+
239+
publicfunctiontestVeryVerboseOutputForSingleMessageContainsExceptionWithTrace()
240+
{
241+
$exception =new \RuntimeException('Things are bad!');
242+
$exceptionLine =__LINE__ -1;
243+
$envelope =newEnvelope(new \stdClass(), [
244+
newTransportMessageIdStamp(15),
245+
newSentToFailureTransportStamp('async'),
246+
newRedeliveryStamp(0),
247+
newErrorDetailsStamp($exception),
248+
]);
249+
$receiver =$this->createMock(ListableReceiverInterface::class);
250+
$receiver->expects($this->once())->method('find')->with(42)->willReturn($envelope);
251+
252+
$command =newFailedMessagesShowCommand('failure_receiver',$receiver);
253+
$tester =newCommandTester($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)

[8]ページ先頭

©2009-2025 Movatter.jp