8000 bug #35137 [Messenger] Added check if json_encode succeeded (toooni) · symfony/symfony@0942e33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0942e33

Browse files
committed
bug #35137 [Messenger] Added check if json_encode succeeded (toooni)
This PR was submitted for the master branch but it was squashed and merged into the 4.4 branch instead (closes #35137). Discussion ---------- [Messenger] Added check if json_encode succeeded | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | When trying to add a message to redis transport which can not be encoded with `json_encode` there is now a `TransportException` containing the `json_last_error_msg` as the message. I had an issue where I tried to send an email through messenger by symfony mailer which contains a pdf attachment. Instead of an error while sending i got an error `Encoded envelope should have at least a "body"` which happened because the encoded message was `false`. This is not exactly a bugfix, but IMO also not a feature worth being mentioned in the changelog so I am not sure I've filled out the Q/A correctly. Commits ------- a16a574 [Messenger] Added check if json_encode succeeded
2 parents 9e7a410 + a16a574 commit 0942e33

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ public function testGetNonBlocking()
186186
$redis->del('messenger-getnonblocking');
187187
}
188188

189+
public function testJsonError()
190+
{
191+
$redis = new \Redis();
192+
193+
$connection = Connection::fromDsn('redis://localhost/json-error', [], $redis);
194+
195+
try {
196+
$connection->add("\xB1\x31", []);
197+
} catch (TransportException $e) {
198+
}
199+
200+
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', $e->getMessage());
201+
}
202+
189203
public function testMaxEntries()
190204
{
191205
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();

src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ public function add(string $body, array $headers, int $delayInMs = 0): void
248248
'uniqid' => uniqid('', true),
249249
]);
250250

251+
if (false === $message) {
252+
throw new TransportException(json_last_error_msg());
253+
}
254+
251255
$score = (int) ($this->getCurrentTimeInMilliseconds() + $delayInMs);
252256
$added = $this->connection->zadd($this->queue, ['NX'], $score, $message);
253257
} else {
@@ -256,6 +260,10 @@ public function add(string $body, array $headers, int $delayInMs = 0): void
256260
'headers' => $headers,
257261
]);
258262

263+
if (false === $message) {
264+
throw new TransportException(json_last_error_msg());
265+
}
266+
259267
if ($this->maxEntries) {
260268
$added = $this->connection->xadd($this->stream, '*', ['message' => $message], $this->maxEntries, true);
261269
} else {

0 commit comments

Comments
 (0)
0