Skip to content

Commit 1740b77

Browse files
author
Илья
committed
Add lambda on exception in retryTransaction
1 parent 9d48501 commit 1740b77

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/Table.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -481,37 +481,43 @@ public function retrySession(Closure $userFunc, bool $idempotent = false, RetryP
481481

482482
}
483483

484-
public function retryTransaction(Closure $userFunc, array|bool $idempotentOrParams = false, RetryParams $params = null){
484+
public function retryTransaction(Closure $userFunc, bool $idempotent = null, RetryParams $params = null, array $options){
485+
if ($options == null) {
486+
$options = [];
487+
}
485488

486-
if (is_array($idempotentOrParams)){
487-
if (isset($idempotentOrParams['idempotent'])){
488-
$idempotentFlag = $idempotentOrParams['idempotent'];
489-
}
490-
if (isset($idempotentOrParams['retry_params'])){
491-
$params = $idempotentOrParams['retry_params'];
492-
}
493-
if (isset($idempotentOrParams['callback_on_error'])){
494-
$callbackOnError = $idempotentOrParams['callback_on_error'];
495-
}
496-
} else {
497-
$idempotentFlag = $idempotentOrParams;
498-
$callbackOnError = function (\Exception $exception){};
489+
if (isset($options['idempotent']) && !is_null($idempotent)){
490+
throw new Exception('...');
491+
}
492+
else if (!isset($options['idempotent'])) {
493+
$options['idempotent'] = $idempotent;
494+
}
495+
496+
if (isset($options['retryParams']) && !is_null($params)){
497+
throw new Exception('...');
498+
}
499+
else if (!isset($options['retryParams'])) {
500+
$options['retryParams'] = $params;
499501
}
500-
return $this->retrySession(function (Session $session) use ($callbackOnError, $userFunc) {
501-
try{
502-
$session->beginTransaction();
503-
$result = $userFunc($session);
504-
$session->commitTransaction();
505-
return $result;
506-
} catch (\Exception $exception){
507-
$callbackOnError($exception);
508-
try {
509-
$session->rollbackTransaction();
510-
} catch (Exception $e){}
511-
throw $exception;
512-
}
513-
}, $idempotentFlag, $params);
514502

503+
if (!isset($options['callback_on_error'])) {
504+
$options['callback_on_error'] = function (\Exception $exception) {};
505+
}
506+
return $this->retrySession(function (Session $session) use ($options, $userFunc) {
507+
try {
508+
$session->beginTransaction();
509+
$result = $userFunc($session);
510+
$session->commitTransaction();
511+
return $result;
512+
} catch (\Exception $exception) {
513+
$options['callback_on_error']($exception);
514+
try {
515+
$session->rollbackTransaction();
516+
} catch (Exception $e) {
517+
}
518+
throw $exception;
519+
}
520+
}, $options['idempotent'], $options['retryParams']);
515521
}
516522

517523
protected function deleteSession(string $exception): bool

0 commit comments

Comments
 (0)