From a80b62c33e29e60782cf0da662d2a4b8a3a25264 Mon Sep 17 00:00:00 2001 From: Dominik Czulak Date: Thu, 22 Feb 2024 11:11:47 +0100 Subject: [PATCH] refactor alibaba exception --- src/Client.php | 4 +-- src/Endpoint/CategoryEndpoint.php | 3 +- src/Exception/AlibabaApiError.php | 32 +++++++++++++++++++ src/Exception/AlibabaException.php | 25 +-------------- .../UnexpectedApiResultException.php | 19 +++++++++++ tests/ClientTest.php | 4 +-- ...eptionTest.php => AlibabaApiErrorTest.php} | 6 ++-- .../UnexpectedApiResultExceptionTest.php | 24 ++++++++++++++ 8 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 src/Exception/AlibabaApiError.php create mode 100644 src/Exception/UnexpectedApiResultException.php rename tests/Exception/{AlibabaExceptionTest.php => AlibabaApiErrorTest.php} (79%) create mode 100644 tests/Exception/UnexpectedApiResultExceptionTest.php diff --git a/src/Client.php b/src/Client.php index 3e897dc..f183bc6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,7 +4,7 @@ namespace Kyto\Alibaba; -use Kyto\Alibaba\Exception\AlibabaException; +use Kyto\Alibaba\Exception\AlibabaApiError; use Kyto\Alibaba\Util\Clock; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -96,7 +96,7 @@ private function throwOnError(array $data): void $errorResponse = $data['error_response'] ?? null; if ($errorResponse !== null) { - throw new AlibabaException( + throw new AlibabaApiError( $errorResponse['msg'], (int) $errorResponse['code'], $errorResponse['sub_msg'], diff --git a/src/Endpoint/CategoryEndpoint.php b/src/Endpoint/CategoryEndpoint.php index cf3f032..5e9d2a7 100644 --- a/src/Endpoint/CategoryEndpoint.php +++ b/src/Endpoint/CategoryEndpoint.php @@ -5,6 +5,7 @@ namespace Kyto\Alibaba\Endpoint; use Kyto\Alibaba\Client; +use Kyto\Alibaba\Exception\UnexpectedApiResultException; use Kyto\Alibaba\Factory\CategoryFactory; use Kyto\Alibaba\Model\Category; use Kyto\Alibaba\Model\CategoryAttribute; @@ -100,7 +101,7 @@ public function getLevelAttribute( ); $attribute = $data['alibaba_icbu_category_level_attr_get_response']['result_list'] - ?? throw new \RuntimeException($errorMessage); + ?? throw new UnexpectedApiResultException($errorMessage); return $this->categoryFactory->createLevelAttribute($attribute); } diff --git a/src/Exception/AlibabaApiError.php b/src/Exception/AlibabaApiError.php new file mode 100644 index 0000000..e538eb7 --- /dev/null +++ b/src/Exception/AlibabaApiError.php @@ -0,0 +1,32 @@ +subCode, $this->subMessage); + parent::__construct($message, $code, $previous); + } + + public function getSubMessage(): string + { + return $this->subMessage; + } + + public function getSubCode(): string + { + return $this->subCode; + } +} diff --git a/src/Exception/AlibabaException.php b/src/Exception/AlibabaException.php index b72068a..24758b2 100644 --- a/src/Exception/AlibabaException.php +++ b/src/Exception/AlibabaException.php @@ -4,29 +4,6 @@ namespace Kyto\Alibaba\Exception; -class AlibabaException extends \RuntimeException +abstract class AlibabaException extends \RuntimeException { - /** - * @internal - */ - public function __construct( - string $message, - int $code, - private string $subMessage, - private string $subCode, - ?\Throwable $previous = null - ) { - $message = sprintf('%s. Sub-code: "%s". Sub-message: "%s".', $message, $this->subCode, $this->subMessage); - parent::__construct($message, $code, $previous); - } - - public function getSubMessage(): string - { - return $this->subMessage; - } - - public function getSubCode(): string - { - return $this->subCode; - } } diff --git a/src/Exception/UnexpectedApiResultException.php b/src/Exception/UnexpectedApiResultException.php new file mode 100644 index 0000000..eef1345 --- /dev/null +++ b/src/Exception/UnexpectedApiResultException.php @@ -0,0 +1,19 @@ +willReturn($response); if (!$isSuccess) { - $this->expectException(AlibabaException::class); + $this->expectException(AlibabaApiError::class); } $actual = $this->client->request(['hello' => 'world', 'test' => 'data']); diff --git a/tests/Exception/AlibabaExceptionTest.php b/tests/Exception/AlibabaApiErrorTest.php similarity index 79% rename from tests/Exception/AlibabaExceptionTest.php rename to tests/Exception/AlibabaApiErrorTest.php index 3af1f40..5cde34b 100644 --- a/tests/Exception/AlibabaExceptionTest.php +++ b/tests/Exception/AlibabaApiErrorTest.php @@ -4,10 +4,10 @@ namespace Kyto\Alibaba\Tests\Exception; -use Kyto\Alibaba\Exception\AlibabaException; +use Kyto\Alibaba\Exception\AlibabaApiError; use PHPUnit\Framework\TestCase; -class AlibabaExceptionTest extends TestCase +class AlibabaApiErrorTest extends TestCase { public function testConstruct(): void { @@ -17,7 +17,7 @@ public function testConstruct(): void $subCode = 'sub.code'; $previous = new \RuntimeException('Previous'); - $exception = new AlibabaException($message, $code, $subMessage, $subCode, $previous); + $exception = new AlibabaApiError($message, $code, $subMessage, $subCode, $previous); self::assertSame('Message. Sub-code: "sub.code". Sub-message: "Sub-message".', $exception->getMessage()); self::assertSame($code, $exception->getCode()); diff --git a/tests/Exception/UnexpectedApiResultExceptionTest.php b/tests/Exception/UnexpectedApiResultExceptionTest.php new file mode 100644 index 0000000..cb81635 --- /dev/null +++ b/tests/Exception/UnexpectedApiResultExceptionTest.php @@ -0,0 +1,24 @@ +getMessage()); + self::assertSame($code, $exception->getCode()); + self::assertSame($previous, $exception->getPrevious()); + } +}