8000 [Messenger] Improve formatting of thrown exception in failed message … · symfony/symfony@2454cdf · GitHub
[go: up one dir, main page]

Skip to content

Commit 2454cdf

Browse files
committed
[Messenger] Improve formatting of thrown exception in failed message information
1 parent 050f3e4 commit 2454cdf

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Helper\Dumper;
1616
use Symfony\Component\Console\Style\SymfonyStyle;
1717
use Symfony\Component\Messenger\Envelope;
18+
use Symfony\Component\Messenger\Exception\ThrownExceptionDetails;
1819
use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
1920
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
2021
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
@@ -130,7 +131,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io)
130131
} elseif (null !== $lastRedeliveryStampWithException) {
131132
$flattenException = $lastRedeliveryStampWithException->getFlattenException();
132133
}
133-
$io->writeln(null === $flattenException ? '(no data)' : $flattenException->getTraceAsString());
134+
$io->writeln(null === $flattenException ? '(no data)' : $dump(ThrownExceptionDetails::createFromFlattenException($flattenException)));
134135
} else {
135136
$io->writeln(' Re-run command with <info>-vv</info> to see more message & error details.');
136137
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Messenger\Exception;
15+
16+
use Symfony\Component\ErrorHandler\Exception\FlattenException;
17+
18+
/**
19+
* ThrownExceptionDetails captures details of a thrown exception and is displayed like a normal exception when dumped
20+
* with the VarDumper.
21+
*
22+
* @author Jeroen Noten <jeroennoten@me.com>
23+
*/
24+
class ThrownExceptionDetails
25+
{
26+
private $class;
27+
/**
28+
* @var string
29+
*/
30+
private $message;
31+
/**
32+
* @var int
33+
*/
34+
private $code;
35+
/**
36+
* @var string
37+
*/
38+
private $file;
39+
/**
40+
* @var int
41+
*/
42+
private $line;
43+
/**
44+
* @var array
45+
*/
46+
private $trace;
47+
48+
private function __construct()
49+
{
50+
}
51+
52+
public static function createFromFlattenException(FlattenException $flattenException): self
53+
{
54+
$thrownExceptionDetails = new self();
55+
$thrownExceptionDetails->class = $flattenException->getClass();
56+
$thrownExceptionDetails->message = $flattenException->getMessage();
57+
$thrownExceptionDetails->code = $flattenException->getCode();
58+
$thrownExceptionDetails->file = $flattenException->getFile();
59+
$thrownExceptionDetails->line = $flattenException->getLine();
60+
$thrownExceptionDetails->trace = $flattenException->getTrace();
61+
62+
return $thrownExceptionDetails;
63+
}
64+
65+
public function getClass(): string
66+
{
67+
return $this->class;
68+
}
69+
70+
public function getMessage(): string
71+
{
72+
return $this->message;
73+
}
74+
75+
public function getCode(): int
76+
{
77+
return $this->code;
78+
}
79+
80+
public function getFile(): string
81+
{
82+
return $this->file;
83+
}
84+
85+
public function getLine(): int
86+
{
87+
return $this->line;
88+
}
89+
90+
public function getTrace(): array
91+
{
92+
return $this->trace;
93+
}
94+
}

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

Lines changed: 37 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,40 @@ public function testInvalidMessagesThrowsException()
234235
$tester = new CommandTester($command);
235236
$tester->execute(['id' => 15]);
236237
}
238+
239+
public function testVeryVerboseOutputForSingleMessageContainsExceptionWithTrace()
240+
{
241+
$exceptionMessage = 'Things are bad!';
242+
$exception = new \RuntimeException($exceptionMessage);
243+
$exceptionLine = __LINE__ - 1;
244+
$envelope = new Envelope(new \stdClass(), [
245+
new TransportMessageIdStamp(15),
246+
new SentToFailureTransportStamp('async'),
247+
new RedeliveryStamp(0),
248+
new ErrorDetailsStamp($exception),
249+
]);
250+
$receiver = $this->createMock(ListableReceiverInterface::class);
251+
$receiver->expects($this->once())->method('find')->with(42)->willReturn($envelope);
252+
253+
$command = new FailedMessagesShowCommand('failure_receiver', $receiver);
254+
$tester = new CommandTester($command);
255+
$tester->execute(['id' => 42], ['verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE]);
256+
$this->assertStringMatchesFormat(sprintf(<<<'EOF'
257+
%%A
258+
Exception:
259+
==========
260+
261+
RuntimeException {
262+
-message: "Things are bad!"
263+
-code: 0
264+
-file: "%s"
265+
-line: %d
266+
trace: {
267+
%%s%%eTests%%eCommand%%eFailedMessagesShowCommandTest.php:%d {
268+
%%A
269+
EOF
270+
,
271+
__FILE__, $exceptionLine, $exceptionLine),
272+
$tester->getDisplay(true));
273+
}
237274
}

src/Symfony/Component/Messenger/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"symfony/serializer": "^4.4|^5.0",
3636
"symfony/service-contracts": "^1.1|^2",
3737
"symfony/stopwatch": "^4.4|^5.0",
38-
"symfony/validator": "^4.4|^5.0"
38+
"symfony/validator": "^4.4|^5.0",
39+
"symfony/var-dumper": "^5.3"
3940
},
4041
"conflict": {
4142
"symfony/event-dispatcher": "<4.4",

0 commit comments

Comments
 (0)
0