From d1c6530759f37c1a20ecaa064985f944e5037966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <38153753+ilyakharev@users.noreply.github.com> Date: Mon, 24 Jul 2023 14:45:16 +0300 Subject: [PATCH 1/4] add class exist check --- src/Traits/RequestTrait.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Traits/RequestTrait.php b/src/Traits/RequestTrait.php index bd33dc60..b47fc975 100644 --- a/src/Traits/RequestTrait.php +++ b/src/Traits/RequestTrait.php @@ -186,6 +186,10 @@ protected function handleGrpcStatus($service, $method, $status) 'credentials' => $this->ydb->iam()->getCredentials() ]); if (isset(self::$grpcExceptions[$status->code])) { + if (!class_exists(self::$grpcExceptions[$status->code])){ + $this->logger->error("Class ".self::$grpcExceptions[$status->code]." not found"); + throw new \Exception($message); + } throw new self::$grpcExceptions[$status->code]($message); } else { throw new \Exception($message); @@ -255,7 +259,11 @@ protected function processResponse($service, $method, $response, $resultClass) ); $msg = 'YDB ' . $service . ' ' . $method . ' (YDB_' . $statusCode . ' ' . $statusName . '): ' . $message; - if (isset(self::$ydbExceptions[$statusCode])) { + if (isset(self::$ydbExceptions[$statusCode]) && class_exists(self::$ydbExceptions[$statusCode])) { + if (!class_exists(self::$ydbExceptions[$status->code])){ + $this->logger->error("Class ".self::$ydbExceptions[$status->code]." not found"); + throw new \Exception($message); + } throw new self::$ydbExceptions[$statusCode]($msg); } else { throw new \Exception($msg); From 3109ae873da55066ec9398dc7a4764cd16923c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <38153753+ilyakharev@users.noreply.github.com> Date: Mon, 24 Jul 2023 14:48:00 +0300 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4770a6cf..bd5d59ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* add class exist check in RequestTrait + # 1.8.1 * fixed bug, when function Retry::backoffType always return SlowBackoff From f3cd05577ed61a0c7050456137761422123ae526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <38153753+ilyakharev@users.noreply.github.com> Date: Mon, 24 Jul 2023 14:54:30 +0300 Subject: [PATCH 3/4] Update RequestTrait.php --- src/Traits/RequestTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/RequestTrait.php b/src/Traits/RequestTrait.php index b47fc975..1f1c3629 100644 --- a/src/Traits/RequestTrait.php +++ b/src/Traits/RequestTrait.php @@ -260,8 +260,8 @@ protected function processResponse($service, $method, $response, $resultClass) $msg = 'YDB ' . $service . ' ' . $method . ' (YDB_' . $statusCode . ' ' . $statusName . '): ' . $message; if (isset(self::$ydbExceptions[$statusCode]) && class_exists(self::$ydbExceptions[$statusCode])) { - if (!class_exists(self::$ydbExceptions[$status->code])){ - $this->logger->error("Class ".self::$ydbExceptions[$status->code]." not found"); + if (!class_exists(self::$ydbExceptions[$statusCode])){ + $this->logger->error("Class ".self::$ydbExceptions[$statusCode]." not found"); throw new \Exception($message); } throw new self::$ydbExceptions[$statusCode]($msg); From dad663d64036cf580270a6b21173896598e2102d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= Date: Mon, 24 Jul 2023 22:41:26 +0300 Subject: [PATCH 4/4] Remove class_exist and refactor test --- src/Traits/RequestTrait.php | 10 +--------- tests/{RetryParams.php => RetryParamsTest.php} | 16 +++++++++------- 2 files changed, 10 insertions(+), 16 deletions(-) rename tests/{RetryParams.php => RetryParamsTest.php} (96%) diff --git a/src/Traits/RequestTrait.php b/src/Traits/RequestTrait.php index 1f1c3629..bd33dc60 100644 --- a/src/Traits/RequestTrait.php +++ b/src/Traits/RequestTrait.php @@ -186,10 +186,6 @@ protected function handleGrpcStatus($service, $method, $status) 'credentials' => $this->ydb->iam()->getCredentials() ]); if (isset(self::$grpcExceptions[$status->code])) { - if (!class_exists(self::$grpcExceptions[$status->code])){ - $this->logger->error("Class ".self::$grpcExceptions[$status->code]." not found"); - throw new \Exception($message); - } throw new self::$grpcExceptions[$status->code]($message); } else { throw new \Exception($message); @@ -259,11 +255,7 @@ protected function processResponse($service, $method, $response, $resultClass) ); $msg = 'YDB ' . $service . ' ' . $method . ' (YDB_' . $statusCode . ' ' . $statusName . '): ' . $message; - if (isset(self::$ydbExceptions[$statusCode]) && class_exists(self::$ydbExceptions[$statusCode])) { - if (!class_exists(self::$ydbExceptions[$statusCode])){ - $this->logger->error("Class ".self::$ydbExceptions[$statusCode]." not found"); - throw new \Exception($message); - } + if (isset(self::$ydbExceptions[$statusCode])) { throw new self::$ydbExceptions[$statusCode]($msg); } else { throw new \Exception($msg); diff --git a/tests/RetryParams.php b/tests/RetryParamsTest.php similarity index 96% rename from tests/RetryParams.php rename to tests/RetryParamsTest.php index 7e49a257..a80df184 100644 --- a/tests/RetryParams.php +++ b/tests/RetryParamsTest.php @@ -18,14 +18,13 @@ use YdbPlatform\Ydb\Exceptions\Grpc\UnavailableException; use YdbPlatform\Ydb\Exceptions\Grpc\UnimplementedException; use YdbPlatform\Ydb\Exceptions\Grpc\UnknownException; -use YdbPlatform\Ydb\Exceptions\RetryableException; use YdbPlatform\Ydb\Exceptions\Ydb\BadRequestException; use YdbPlatform\Ydb\Exceptions\Ydb\InternalErrorException; use YdbPlatform\Ydb\Exceptions\Ydb\StatusCodeUnspecified; use YdbPlatform\Ydb\Exceptions\Ydb\UnauthorizedException; +use YdbPlatform\Ydb\Logger\SimpleStdLogger; use YdbPlatform\Ydb\Retry\Backoff; use YdbPlatform\Ydb\Retry\RetryParams; -use YdbPlatform\Ydb\Session; use YdbPlatform\Ydb\Table; use YdbPlatform\Ydb\Ydb; use YdbPlatform\Ydb\Retry\Retry; @@ -48,7 +47,7 @@ public function deleteSession(string $exception): bool } } -class RetryTest2 extends \PHPUnit\Framework\TestCase +class RetryParamsTest extends \PHPUnit\Framework\TestCase { protected const FAST = 5; protected const SLOW = 20; @@ -370,22 +369,25 @@ public function test(){ $retryParams = new RetryParams(1000, new Backoff(6,self::FAST), new Backoff(6,self::SLOW)); - $retry = (new RetrySubclass())->withParams($retryParams); - $table = new TableSubclass(new Ydb(["credentials"=>new AnonymousAuthentication()]), null, $retry); + $logger = new SimpleStdLogger(7); + $retry = (new RetrySubclass($logger))->withParams($retryParams); + $table = new TableSubclass(new Ydb(["credentials"=>new AnonymousAuthentication()]), $logger, $retry); foreach ($this->errsToCheck as $error) { + $exception = new $error["class"](); + $resultDeleteSession = $table->deleteSession($error["class"]) ? "true" : "false"; $wantDeleteSession = $error["deleteSession"] ? "true" : "false"; self::assertEquals($wantDeleteSession, $resultDeleteSession, "{$error["class"]}: unexpected delete session status: $resultDeleteSession, want: $wantDeleteSession"); - $resultRetryIdempotent = $retry->canRetry(new $error["class"](), true) ? "true" : "false"; + $resultRetryIdempotent = $retry->canRetry($exception, true) ? "true" : "false"; $wantRetryIdempotent = $error["retry"]["idempotent"] ? "true" : "false"; self::assertEquals($wantRetryIdempotent, $resultRetryIdempotent, "{$error["class"]}: unexpected must retry idempotent operation status: $resultRetryIdempotent, want: $wantRetryIdempotent"); - $resultRetryNonIdempotent = $retry->canRetry(new $error["class"](), false) ? "true" : "false"; + $resultRetryNonIdempotent = $retry->canRetry($exception, false) ? "true" : "false"; $wantRetryNonIdempotent = $error["retry"]["nonIdempotent"] ? "true" : "false"; self::assertEquals($wantRetryNonIdempotent, $resultRetryNonIdempotent, "{$error["class"]}: unexpected must retry non-idempotent operation status: $resultDeleteSession, want: $wantDeleteSession");