diff --git a/src/Symfony/Component/Lock/Store/RedisStore.php b/src/Symfony/Component/Lock/Store/RedisStore.php index a7bf7fec29de6..503d3067bf560 100644 --- a/src/Symfony/Component/Lock/Store/RedisStore.php +++ b/src/Symfony/Component/Lock/Store/RedisStore.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Lock\Store; -use Predis\Response\ServerException; +use Predis\Response\Error; use Relay\Relay; use Symfony\Component\Lock\Exception\InvalidTtlException; use Symfony\Component\Lock\Exception\LockConflictedException; @@ -250,10 +250,10 @@ private function evaluate(string $script, string $resource, array $args): mixed } $result = $this->redis->evalSha($scriptSha, array_merge([$resource], $args), 1); + } - if (null !== $err = $this->redis->getLastError()) { - throw new LockStorageException($err); - } + if (null !== $err = $this->redis->getLastError()) { + throw new LockStorageException($err); } return $result; @@ -273,10 +273,10 @@ private function evaluate(string $script, string $resource, array $args): mixed } $result = $client->evalSha($scriptSha, array_merge([$resource], $args), 1); + } - if (null !== $err = $client->getLastError()) { - throw new LockStorageException($err); - } + if (null !== $err = $client->getLastError()) { + throw new LockStorageException($err); } return $result; @@ -284,26 +284,21 @@ private function evaluate(string $script, string $resource, array $args): mixed \assert($this->redis instanceof \Predis\ClientInterface); - try { - return $this->redis->evalSha($scriptSha, 1, $resource, ...$args); - } catch (ServerException $e) { - // Fallthrough only if we need to load the script - if (self::NO_SCRIPT_ERROR_MESSAGE !== $e->getMessage()) { - throw new LockStorageException($e->getMessage(), $e->getCode(), $e); + $result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args); + if ($result instanceof Error && self::NO_SCRIPT_ERROR_MESSAGE === $result->getMessage()) { + $result = $this->redis->script('LOAD', $script); + if ($result instanceof Error) { + throw new LockStorageException($result->getMessage()); } - } - try { - $this->redis->script('LOAD', $script); - } catch (ServerException $e) { - throw new LockStorageException($e->getMessage(), $e->getCode(), $e); + $result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args); } - try { - return $this->redis->evalSha($scriptSha, 1, $resource, ...$args); - } catch (ServerException $e) { - throw new LockStorageException($e->getMessage(), $e->getCode(), $e); + if ($result instanceof Error) { + throw new LockStorageException($result->getMessage()); } + + return $result; } private function getUniqueToken(Key $key): string diff --git a/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php index b7cdd7a954a94..6a19805f3cb3e 100644 --- a/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php @@ -39,7 +39,10 @@ protected function getClockDelay(): int public function getStore(): PersistingStoreInterface { - $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379])); + $redis = new \Predis\Client( + array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]), + ['exceptions' => false], + ); try { $redis->connect(); diff --git a/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php index 3569e3d1f75e2..74a72b5a4003a 100644 --- a/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php @@ -20,7 +20,10 @@ class PredisStoreTest extends AbstractRedisStoreTestCase { public static function setUpBeforeClass(): void { - $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null])); + $redis = new \Predis\Client( + array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]), + ['exceptions' => false], + ); try { $redis->connect(); } catch (\Exception $e) { @@ -30,7 +33,10 @@ public static function setUpBeforeClass(): void protected function getRedisConnection(): \Predis\Client { - $redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null])); + $redis = new \Predis\Client( + array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]), + ['exceptions' => false], + ); $redis->connect(); return $redis;