Skip to content

Commit

Permalink
bug #35137 [Messenger] Added check if json_encode succeeded (toooni)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jan 7, 2020
2 parents 9e7a410 + a16a574 commit 0942e33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Expand Up @@ -186,6 +186,20 @@ 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 testMaxEntries()
{
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
Expand Down
Expand Up @@ -248,6 +248,10 @@ public function add(string $body, array $headers, int $delayInMs = 0): void
'uniqid' => uniqid('', true),
]);

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

$score = (int) ($this->getCurrentTimeInMilliseconds() + $delayInMs);
$added = $this->connection->zadd($this->queue, ['NX'], $score, $message);
} else {
Expand All @@ -256,6 +260,10 @@ public function add(string $body, array $headers, int $delayInMs = 0): void
'headers' => $headers,
]);

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

if ($this->maxEntries) {
$added = $this->connection->xadd($this->stream, '*', ['message' => $message], $this->maxEntries, true);
} else {
Expand Down

0 comments on commit 0942e33

Please sign in to comment.