Skip to content

Commit ac8daf1

Browse files
author
Robin Chalas
committed
[Messenger] Ensure that a TransportException is thrown on redis error
1 parent a8a1e7a commit ac8daf1

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

Tests/Transport/RedisExt/ConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Messenger\Tests\Transport\RedisExt;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Messenger\Exception\LogicException;
15+
use Symfony\Component\Messenger\Exception\TransportException;
1616
use Symfony\Component\Messenger\Transport\RedisExt\Connection;
1717

1818
/**
@@ -103,7 +103,7 @@ public function testFirstGetPendingMessagesThenNewMessages()
103103

104104
public function testUnexpectedRedisError()
105105
{
106-
$this->expectException(LogicException::class);
106+
$this->expectException(TransportException::class);
107107
$this->expectExceptionMessage('Redis error happens');
108108
$redis = $this->getMockBuilder(\Redis::class)->disableOriginalConstructor()->getMock();
109109
$redis->expects($this->once())->method('xreadgroup')->willReturn(false);

Transport/RedisExt/Connection.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Messenger\Transport\RedisExt;
1313

1414
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
15-
use Symfony\Component\Messenger\Exception\LogicException;
15+
use Symfony\Component\Messenger\Exception\TransportException;
1616

1717
/**
1818
* A Redis connection.
@@ -77,17 +77,21 @@ public function get(): ?array
7777
$messageId = '0'; // will receive consumers pending messages
7878
}
7979

80-
$messages = $this->connection->xreadgroup(
81-
$this->group,
82-
$this->consumer,
83-
[$this->stream => $messageId],
84-
1,
85-
$this->blockingTimeout
86-
);
87-
88-
if (false === $messages) {
89-
throw new LogicException(
90-
$this->connection->getLastError() ?: 'Unexpected redis stream error happened.'
80+
$e = null;
81+
try {
82+
$messages = $this->connection->xReadGroup(
83+
$this->group,
84+
$this->consumer,
85+
[$this->stream => $messageId],
86+
1,
87+
$this->blockingTimeout
88+
);
89+
} catch (\RedisException $e) {
90+
}
91+
92+
if (false === $messages || $e) {
93+
throw new TransportException(
94+
($e ? $e->getMessage() : $this->connection->getLastError()) ?? 'Could not read messages from the redis stream.'
9195
);
9296
}
9397

@@ -113,23 +117,51 @@ public function get(): ?array
113117

114118
public function ack(string $id): void
115119
{
116-
$this->connection->xack($this->stream, $this->group, [$id]);
120+
$e = null;
121+
try {
122+
$acknowledged = $this->connection->xAck($this->stream, $this->group, [$id]);
123+
} catch (\RedisException $e) {
124+
}
125+
126+
if (!$acknowledged || $e) {
127+
throw new TransportException(($e ? $e->getMessage() : $this->connection->getLastError()) ?? sprintf('Could not acknowledge redis message "%s".', $id), 0, $e);
128+
}
117129
}
118130

119131
public function reject(string $id): void
120132
{
121-
$this->connection->xdel($this->stream, [$id]);
133+
$e = null;
134+
try {
135+
$deleted = $this->connection->xDel($this->stream, [$id]);
136+
} catch (\RedisException $e) {
137+
}
138+
139+
if (!$deleted || $e) {
140+
throw new TransportException(($e ? $e->getMessage() : $this->connection->getLastError()) ?? sprintf('Could not delete message "%s" from the redis stream.', $id), 0, $e);
141+
}
122142
}
123143

124144
public function add(string $body, array $headers)
125145
{
126-
$this->connection->xadd($this->stream, '*', ['message' => json_encode(
127-
['body' => $body, 'headers' => $headers]
128-
)]);
146+
$e = null;
147+
try {
148+
$added = $this->connection->xAdd($this->stream, '*', ['message' => json_encode(
149+
['body' => $body, 'headers' => $headers]
150+
)]);
151+
} catch (\RedisException $e) {
152+
}
153+
154+
if (!$added || $e) {
155+
throw new TransportException(($e ? $e->getMessage() : $this->connection->getLastError()) ?? 'Could not add a message to the redis stream.', 0, $e);
156+
}
129157
}
130158

131159
public function setup(): void
132160
{
133-
$this->connection->xgroup('CREATE', $this->stream, $this->group, 0, true);
161+
try {
162+
$this->connection->xGroup('CREATE', $this->stream, $this->group, 0, true);
163+
} catch (\RedisException $e) {
164+
throw new TransportException($e->getMessage(), 0, $e);
165+
}
134166
}
135167
}

0 commit comments

Comments
 (0)