Skip to content

Commit d0cfffb

Browse files
Fix predis command error checking
1 parent 07212a5 commit d0cfffb

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

Store/RedisStore.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Lock\Store;
1313

14-
use Predis\Response\ServerException;
14+
use Predis\Response\Error;
1515
use Relay\Relay;
1616
use Symfony\Component\Lock\Exception\InvalidTtlException;
1717
use Symfony\Component\Lock\Exception\LockConflictedException;
@@ -250,10 +250,10 @@ private function evaluate(string $script, string $resource, array $args): mixed
250250
}
251251

252252
$result = $this->redis->evalSha($scriptSha, array_merge([$resource], $args), 1);
253+
}
253254

254-
if (null !== $err = $this->redis->getLastError()) {
255-
throw new LockStorageException($err);
256-
}
255+
if (null !== $err = $this->redis->getLastError()) {
256+
throw new LockStorageException($err);
257257
}
258258

259259
return $result;
@@ -273,37 +273,32 @@ private function evaluate(string $script, string $resource, array $args): mixed
273273
}
274274

275275
$result = $client->evalSha($scriptSha, array_merge([$resource], $args), 1);
276+
}
276277

277-
if (null !== $err = $client->getLastError()) {
278-
throw new LockStorageException($err);
279-
}
278+
if (null !== $err = $client->getLastError()) {
279+
throw new LockStorageException($err);
280280
}
281281

282282
return $result;
283283
}
284284

285285
\assert($this->redis instanceof \Predis\ClientInterface);
286286

287-
try {
288-
return $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
289-
} catch (ServerException $e) {
290-
// Fallthrough only if we need to load the script
291-
if (self::NO_SCRIPT_ERROR_MESSAGE !== $e->getMessage()) {
292-
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
287+
$result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
288+
if ($result instanceof Error && self::NO_SCRIPT_ERROR_MESSAGE === $result->getMessage()) {
289+
$result = $this->redis->script('LOAD', $script);
290+
if ($result instanceof Error) {
291+
throw new LockStorageException($result->getMessage());
293292
}
294-
}
295293

296-
try {
297-
$this->redis->script('LOAD', $script);
298-
} catch (ServerException $e) {
299-
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
294+
$result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
300295
}
301296

302-
try {
303-
return $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
304-
} catch (ServerException $e) {
305-
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
297+
if ($result instanceof Error) {
298+
throw new LockStorageException($result->getMessage());
306299
}
300+
301+
return $result;
307302
}
308303

309304
private function getUniqueToken(Key $key): string

Tests/Store/CombinedStoreTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ protected function getClockDelay(): int
3939

4040
public function getStore(): PersistingStoreInterface
4141
{
42-
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]));
42+
$redis = new \Predis\Client(
43+
array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]),
44+
['exceptions' => false],
45+
);
4346

4447
try {
4548
$redis->connect();

Tests/Store/PredisStoreTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ class PredisStoreTest extends AbstractRedisStoreTestCase
2020
{
2121
public static function setUpBeforeClass(): void
2222
{
23-
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
23+
$redis = new \Predis\Client(
24+
array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]),
25+
['exceptions' => false],
26+
);
2427
try {
2528
$redis->connect();
2629
} catch (\Exception $e) {
@@ -30,7 +33,10 @@ public static function setUpBeforeClass(): void
3033

3134
protected function getRedisConnection(): \Predis\Client
3235
{
33-
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
36+
$redis = new \Predis\Client(
37+
array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]),
38+
['exceptions' => false],
39+
);
3440
$redis->connect();
3541

3642
return $redis;

0 commit comments

Comments
 (0)