|
12 | 12 | namespace Symfony\Component\Lock\Store;
|
13 | 13 |
|
14 | 14 | use Predis\Response\Error;
|
| 15 | +use Predis\Response\ServerException; |
15 | 16 | use Relay\Relay;
|
16 | 17 | use Symfony\Component\Lock\Exception\InvalidTtlException;
|
17 | 18 | use Symfony\Component\Lock\Exception\LockConflictedException;
|
@@ -284,21 +285,18 @@ private function evaluate(string $script, string $resource, array $args): mixed
|
284 | 285 |
|
285 | 286 | \assert($this->redis instanceof \Predis\ClientInterface);
|
286 | 287 |
|
287 |
| - $result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args); |
288 |
| - if ($result instanceof Error && str_starts_with($result->getMessage(), self::NO_SCRIPT_ERROR_MESSAGE_PREFIX)) { |
289 |
| - $result = $this->redis->script('LOAD', $script); |
290 |
| - if ($result instanceof Error) { |
291 |
| - throw new LockStorageException($result->getMessage()); |
| 288 | + try { |
| 289 | + return $this->handlePredisError(fn () => $this->redis->evalSha($scriptSha, 1, $resource, ...$args)); |
| 290 | + } catch (LockStorageException $e) { |
| 291 | + // Fallthrough only if we need to load the script |
| 292 | + if (!str_starts_with($e->getMessage(), self::NO_SCRIPT_ERROR_MESSAGE_PREFIX)) { |
| 293 | + throw $e; |
292 | 294 | }
|
293 |
| - |
294 |
| - $result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args); |
295 | 295 | }
|
296 | 296 |
|
297 |
| - if ($result instanceof Error) { |
298 |
| - throw new LockStorageException($result->getMessage()); |
299 |
| - } |
| 297 | + $this->handlePredisError(fn () => $this->redis->script('LOAD', $script)); |
300 | 298 |
|
301 |
| - return $result; |
| 299 | + return $this->handlePredisError(fn () => $this->redis->evalSha($scriptSha, 1, $resource, ...$args)); |
302 | 300 | }
|
303 | 301 |
|
304 | 302 | private function getUniqueToken(Key $key): string
|
@@ -347,4 +345,26 @@ private function getNowCode(): string
|
347 | 345 | now = math.floor(now * 1000)
|
348 | 346 | ';
|
349 | 347 | }
|
| 348 | + |
| 349 | + /** |
| 350 | + * @template T |
| 351 | + * |
| 352 | + * @param callable(): T $callback |
| 353 | + * |
| 354 | + * @return T |
| 355 | + */ |
| 356 | + private function handlePredisError(callable $callback): mixed |
| 357 | + { |
| 358 | + try { |
| 359 | + $result = $callback(); |
| 360 | + } catch (ServerException $e) { |
| 361 | + throw new LockStorageException($e->getMessage(), $e->getCode(), $e); |
| 362 | + } |
| 363 | + |
| 364 | + if ($result instanceof Error) { |
| 365 | + throw new LockStorageException($result->getMessage()); |
| 366 | + } |
| 367 | + |
| 368 | + return $result; |
| 369 | + } |
350 | 370 | }
|
0 commit comments