Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47a866b

Browse files
committed
bug #35150 [Messenger] Added check if json_encode succeeded (toooni)
This PR was squashed before being merged into the 4.3 branch (closes #35150). Discussion ---------- [Messenger] Added check if json_encode succeeded | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Similar PR as #35137 but for branch 4.3. 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 ------- c2bdc4c [Messenger] Added check if json_encode succeeded
2 parents de34f22 + c2bdc4c commit 47a866b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ public function testGetNonBlocking()
177177
$redis->del('messenger-getnonblocking');
178178
}
179179

180+
public function testJsonError()
181+
{
182+
$redis = new \Redis();
183+
$connection = Connection::fromDsn('redis://localhost/json-error', [], $redis);
184+
try {
185+
$connection->add("\xB1\x31", []);
186+
} catch (TransportException $e) {
187+
}
188+
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', $e->getMessage());
189+
}
190+
180191
public function testLastErrorGetsCleared()
181192
{
182193
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,16 @@ public function add(string $body, array $headers): void
185185
}
186186

187187
try {
188-
$added = $this->connection->xadd($this->stream, '*', ['message' => json_encode(
189-
['body' => $body, 'headers' => $headers]
190-
)]);
188+
$message = json_encode([
189+
'body' => $body,
190+
'headers' => $headers,
191+
]);
192+
193+
if (false === $message) {
194+
throw new TransportException(json_last_error_msg());
195+
}
196+
197+
$added = $this->connection->xadd($this->stream, '*', ['message' => $message]);
191198
} catch (\RedisException $e) {
192199
throw new TransportException($e->getMessage(), 0, $e);
193200
}

0 commit comments

Comments
 (0)