Skip to content

Commit 9af554c

Browse files
Jean85fabpot
authored andcommitted
[Messenger] Fix JSON deserialization of ErrorDetailsStamp and normalization of FlattenException::$statusText
1 parent dc0d45d commit 9af554c

File tree

7 files changed

+59
-17
lines changed

7 files changed

+59
-17
lines changed

src/Symfony/Component/Messenger/EventListener/AddErrorDetailsStampListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class AddErrorDetailsStampListener implements EventSubscriberInterface
1919
{
2020
public function onMessageFailed(WorkerMessageFailedEvent $event): void
2121
{
22-
$stamp = new ErrorDetailsStamp($event->getThrowable());
22+
$stamp = ErrorDetailsStamp::create($event->getThrowable());
2323
$previousStamp = $event->getEnvelope()->last(ErrorDetailsStamp::class);
2424

2525
// Do not append duplicate information

src/Symfony/Component/Messenger/Stamp/ErrorDetailsStamp.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,29 @@ final class ErrorDetailsStamp implements StampInterface
3232
/** @var FlattenException|null */
3333
private $flattenException;
3434

35-
public function __construct(Throwable $throwable)
35+
/**
36+
* @param int|mixed $exceptionCode
37+
*/
38+
public function __construct(string $exceptionClass, $exceptionCode, string $exceptionMessage, FlattenException $flattenException = null)
39+
{
40+
$this->exceptionClass = $exceptionClass;
41+
$this->exceptionCode = $exceptionCode;
42+
$this->exceptionMessage = $exceptionMessage;
43+
$this->flattenException = $flattenException;
44+
}
45+
46+
public static function create(Throwable $throwable): self
3647
{
3748
if ($throwable instanceof HandlerFailedException) {
3849
$throwable = $throwable->getPrevious();
3950
}
4051

41-
$this->exceptionClass = \get_class($throwable);
42-
$this->exceptionCode = $throwable->getCode();
43-
$this->exceptionMessage = $throwable->getMessage();
44-
52+
$flattenException = null;
4553
if (class_exists(FlattenException::class)) {
46-
$this->flattenException = FlattenException::createFromThrowable($throwable);
54+
$flattenException = FlattenException::createFromThrowable($throwable);
4755
}
56+
57+
return new self(\get_class($throwable), $throwable->getCode(), $throwable->getMessage(), $flattenException);
4858
}
4959

5060
public function getExceptionClass(): string

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testBasicRun()
3232
{
3333
$sentToFailureStamp = new SentToFailureTransportStamp('async');
3434
$redeliveryStamp = new RedeliveryStamp(0);
35-
$errorStamp = new ErrorDetailsStamp(new \Exception('Things are bad!', 123));
35+
$errorStamp = ErrorDetailsStamp::create(new \Exception('Things are bad!', 123));
3636
$envelope = new Envelope(new \stdClass(), [
3737
new TransportMessageIdStamp(15),
3838
$sentToFailureStamp,
@@ -69,7 +69,7 @@ public function testMultipleRedeliveryFails()
6969
{
7070
$sentToFailureStamp = new SentToFailureTransportStamp('async');
7171
$redeliveryStamp1 = new RedeliveryStamp(0);
72-
$errorStamp = new ErrorDetailsStamp(new \Exception('Things are bad!', 123));
72+
$errorStamp = ErrorDetailsStamp::create(new \Exception('Things are bad!', 123));
7373
$redeliveryStamp2 = new RedeliveryStamp(0);
7474
$envelope = new Envelope(new \stdClass(), [
7575
new TransportMessageIdStamp(15),
@@ -154,7 +154,7 @@ public function testListMessages()
154154
{
155155
$sentToFailureStamp = new SentToFailureTransportStamp('async');
156156
$redeliveryStamp = new RedeliveryStamp(0);
157-
$errorStamp = new ErrorDetailsStamp(new \RuntimeException('Things are bad!'));
157+
$errorStamp = ErrorDetailsStamp::create(new \RuntimeException('Things are bad!'));
158158
$envelope = new Envelope(new \stdClass(), [
159159
new TransportMessageIdStamp(15),
160160
$sentToFailureStamp,
@@ -201,7 +201,7 @@ public function testListMessagesReturnsPaginatedMessages()
201201
new TransportMessageIdStamp(15),
202202
$sentToFailureStamp,
203203
new RedeliveryStamp(0),
204-
new ErrorDetailsStamp(new \RuntimeException('Things are bad!')),
204+
ErrorDetailsStamp::create(new \RuntimeException('Things are bad!')),
205205
]);
206206
$receiver = $this->createMock(ListableReceiverInterface::class);
207207
$receiver->expects($this->once())->method('all')->with()->willReturn([$envelope]);
@@ -244,7 +244,7 @@ public function testVeryVerboseOutputForSingleMessageContainsExceptionWithTrace(
244244
new TransportMessageIdStamp(15),
245245
new SentToFailureTransportStamp('async'),
246246
new RedeliveryStamp(0),
247-
new ErrorDetailsStamp($exception),
247+
ErrorDetailsStamp::create($exception),
248248
]);
249249
$receiver = $this->createMock(ListableReceiverInterface::class);
250250
$receiver->expects($this->once())->method('find')->with(42)->willReturn($envelope);

src/Symfony/Component/Messenger/Tests/EventListener/AddErrorDetailsStampListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testExceptionDetailsAreAdded(): void
1717
$envelope = new Envelope(new \stdClass());
1818
$exception = new \Exception('It failed!');
1919
$event = new WorkerMessageFailedEvent($envelope, 'my_receiver', $exception);
20-
$expectedStamp = new ErrorDetailsStamp($exception);
20+
$expectedStamp = ErrorDetailsStamp::create($exception);
2121

2222
$listener->onMessageFailed($event);
2323

@@ -29,12 +29,12 @@ public function testWorkerAddsNewErrorDetailsStampOnFailure()
2929
$listener = new AddErrorDetailsStampListener();
3030

3131
$envelope = new Envelope(new \stdClass(), [
32-
new ErrorDetailsStamp(new \InvalidArgumentException('First error!')),
32+
ErrorDetailsStamp::create(new \InvalidArgumentException('First error!')),
3333
]);
3434

3535
$exception = new \Exception('Second error!');
3636
$event = new WorkerMessageFailedEvent($envelope, 'my_receiver', $exception);
37-
$expectedStamp = new ErrorDetailsStamp($exception);
37+
$expectedStamp = ErrorDetailsStamp::create($exception);
3838

3939
$listener->onMessageFailed($event);
4040

src/Symfony/Component/Messenger/Tests/Stamp/ErrorDetailsStampTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
use Symfony\Component\Messenger\Envelope;
1717
use Symfony\Component\Messenger\Exception\HandlerFailedException;
1818
use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
19+
use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer;
20+
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
21+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
22+
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
23+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
24+
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
1925

2026
class ErrorDetailsStampTest extends TestCase
2127
{
@@ -24,7 +30,7 @@ public function testGetters(): void
2430
$exception = new \Exception('exception message');
2531
$flattenException = FlattenException::createFromThrowable($exception);
2632

27-
$stamp = new ErrorDetailsStamp($exception);
33+
$stamp = ErrorDetailsStamp::create($exception);
2834

2935
$this->assertSame(\Exception::class, $stamp->getExceptionClass());
3036
$this->assertSame('exception message', $stamp->getExceptionMessage());
@@ -38,11 +44,30 @@ public function testUnwrappingHandlerFailedException(): void
3844
$exception = new HandlerFailedException($envelope, [$wrappedException]);
3945
$flattenException = FlattenException::createFromThrowable($wrappedException);
4046

41-
$stamp = new ErrorDetailsStamp($exception);
47+
$stamp = ErrorDetailsStamp::create($exception);
4248

4349
$this->assertSame(\Exception::class, $stamp->getExceptionClass());
4450
$this->assertSame('I am inside', $stamp->getExceptionMessage());
4551
$this->assertSame(123, $stamp->getExceptionCode());
4652
$this->assertEquals($flattenException, $stamp->getFlattenException());
4753
}
54+
55+
public function testDeserialization(): void
56+
{
57+
$exception = new \Exception('exception message');
58+
$stamp = ErrorDetailsStamp::create($exception);
59+
$serializer = new Serializer(
60+
new SymfonySerializer([
61+
new ArrayDenormalizer(),
62+
new FlattenExceptionNormalizer(),
63+
new ObjectNormalizer(),
64+
], [new JsonEncoder()])
65+
);
66+
67+
$deserializedEnvelope = $serializer->decode($serializer->encode(new Envelope(new \stdClass(), [$stamp])));
68+
69+
$deserializedStamp = $deserializedEnvelope->last(ErrorDetailsStamp::class);
70+
$this->assertInstanceOf(ErrorDetailsStamp::class, $deserializedStamp);
71+
$this->assertEquals($stamp, $deserializedStamp);
72+
}
4873
}

src/Symfony/Component/Messenger/Tests/Transport/Serialization/Normalizer/FlattenExceptionNormalizerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testNormalize(FlattenException $exception)
6060
$this->assertSame($previous, $normalized['previous']);
6161
$this->assertSame($exception->getTrace(), $normalized['trace']);
6262
$this->assertSame($exception->getTraceAsString(), $normalized['trace_as_string']);
63+
$this->assertSame($exception->getStatusText(), $normalized['status_text']);
6364
}
6465

6566
public function provideFlattenException(): array
@@ -95,6 +96,7 @@ public function testDenormalizeValidData()
9596
'file' => 'foo.php',
9697
'line' => 123,
9798
'headers' => ['Content-Type' => 'application/json'],
99+
'status_text' => 'Whoops, looks like something went wrong.',
98100
'trace' => [
99101
[
100102
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123, 'args' => [],
@@ -108,6 +110,7 @@ public function testDenormalizeValidData()
108110
],
109111
],
110112
'trace_as_string' => '#0 foo.php(123): foo()'.\PHP_EOL.'#1 bar.php(456): bar()',
113+
'status_text' => 'Whoops, looks like something went wrong.',
111114
];
112115
$exception = $this->normalizer->denormalize($normalized, FlattenException::class);
113116

@@ -121,10 +124,12 @@ public function testDenormalizeValidData()
121124
$this->assertSame($normalized['line'], $exception->getLine());
122125
$this->assertSame($normalized['trace'], $exception->getTrace());
123126
$this->assertSame($normalized['trace_as_string'], $exception->getTraceAsString());
127+
$this->assertSame($normalized['status_text'], $exception->getStatusText());
124128

125129
$this->assertInstanceOf(FlattenException::class, $previous = $exception->getPrevious());
126130
$this->assertSame($normalized['previous']['message'], $previous->getMessage());
127131
$this->assertSame($normalized['previous']['code'], $previous->getCode());
132+
$this->assertSame($normalized['previous']['status_text'], $previous->getStatusText());
128133
}
129134

130135
private function getMessengerContext(): array

src/Symfony/Component/Messenger/Transport/Serialization/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function normalize($object, $format = null, array $context = [])
4242
'file' => $object->getFile(),
4343
'line' => $object->getLine(),
4444
'previous' => null === $object->getPrevious() ? null : $this->normalize($object->getPrevious(), $format, $context),
45+
'status_text' => $object->getStatusText(),
4546
'trace' => $object->getTrace(),
4647
'trace_as_string' => $object->getTraceAsString(),
4748
];
@@ -73,6 +74,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
7374
$object->setClass($data['class']);
7475
$object->setFile($data['file']);
7576
$object->setLine($data['line']);
77+
$object->setStatusText($data['status_text']);
7678
$object->setHeaders((array) $data['headers']);
7779

7880
if (isset($data['previous'])) {

0 commit comments

Comments
 (0)