Skip to content

Commit

Permalink
bug #35150 [Messenger] Added check if json_encode succeeded (toooni)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jan 7, 2020
2 parents de34f22 + c2bdc4c commit 47a866b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Expand Up @@ -177,6 +177,17 @@ public function testGetNonBlocking()
$redis->del('messenger-getnonblocking');
}

public function testJsonError()
{
$redis = new \Redis();
$connection = Connection::fromDsn('redis://localhost/json-error', [], $redis);
try {
$connection->add("\xB1\x31", []);
} catch (TransportException $e) {
}
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', $e->getMessage());
}

public function testLastErrorGetsCleared()
{
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php
Expand Up @@ -185,9 +185,16 @@ public function add(string $body, array $headers): void
}

try {
$added = $this->connection->xadd($this->stream, '*', ['message' => json_encode(
['body' => $body, 'headers' => $headers]
)]);
$message = json_encode([
'body' => $body,
'headers' => $headers,
]);

if (false === $message) {
throw new TransportException(json_last_error_msg());
}

$added = $this->connection->xadd($this->stream, '*', ['message' => $message]);
} catch (\RedisException $e) {
throw new TransportException($e->getMessage(), 0, $e);
}
Expand Down

0 comments on commit 47a866b

Please sign in to comment.