8000 [Messenger] Added check if json_encode succeeded · symfony/symfony@a16a574 · GitHub
[go: up one dir, main page]

Skip to content

Commit a16a574

Browse files
tooonifabpot
authored andcommitted
[Messenger] Added check if json_encode succeeded
1 parent 9e7a410 commit a16a574

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