From 12fcfdb721ef4ac943ec1d5c3b0358ac0da2ae53 Mon Sep 17 00:00:00 2001 From: Dominik Czulak Date: Tue, 20 Feb 2024 13:24:11 +0100 Subject: [PATCH] handle empty result, revert alibaba nullable error codes --- src/Client.php | 7 ++----- src/Endpoint/CategoryEndpoint.php | 11 ++++++++++- src/Exception/AlibabaException.php | 13 +++++-------- tests/Factory/CategoryFactoryTest.php | 8 ++++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/Client.php b/src/Client.php index 080c9df..3e897dc 100644 --- a/src/Client.php +++ b/src/Client.php @@ -96,14 +96,11 @@ private function throwOnError(array $data): void $errorResponse = $data['error_response'] ?? null; if ($errorResponse !== null) { - $subMessage = $errorResponse['sub_msg'] ?? null; - $subCode = $errorResponse['sub_code'] ?? null; - throw new AlibabaException( $errorResponse['msg'], (int) $errorResponse['code'], - $subMessage, - $subCode, + $errorResponse['sub_msg'], + $errorResponse['sub_code'], ); } } diff --git a/src/Endpoint/CategoryEndpoint.php b/src/Endpoint/CategoryEndpoint.php index c920dae..cf3f032 100644 --- a/src/Endpoint/CategoryEndpoint.php +++ b/src/Endpoint/CategoryEndpoint.php @@ -92,7 +92,16 @@ public function getLevelAttribute( 'attribute_value_request' => json_encode($attributeValueRequest) ]); - $attribute = $data['alibaba_icbu_category_level_attr_get_response']['result_list']; + $errorMessage = sprintf( + 'Result list for category id: "%s", attribute id: "%s", value id: "%s" is empty.', + $categoryId, + $attributeId, + $valueId + ); + + $attribute = $data['alibaba_icbu_category_level_attr_get_response']['result_list'] + ?? throw new \RuntimeException($errorMessage); + return $this->categoryFactory->createLevelAttribute($attribute); } } diff --git a/src/Exception/AlibabaException.php b/src/Exception/AlibabaException.php index de6d2f6..b72068a 100644 --- a/src/Exception/AlibabaException.php +++ b/src/Exception/AlibabaException.php @@ -12,23 +12,20 @@ class AlibabaException extends \RuntimeException public function __construct( string $message, int $code, - private ?string $subMessage, - private ?string $subCode, + private string $subMessage, + private string $subCode, ?\Throwable $previous = null ) { - $subCodePart = $this->subCode !== null ? sprintf(' Sub-code: "%s".', $this->subCode) : null; - $subMessagePart = $this->subMessage !== null ? sprintf(' Sub-message: "%s".', $this->subMessage) : null; - $message = sprintf('%s.%s%s', $message, $subCodePart, $subMessagePart); - + $message = sprintf('%s. Sub-code: "%s". Sub-message: "%s".', $message, $this->subCode, $this->subMessage); parent::__construct($message, $code, $previous); } - public function getSubMessage(): ?string + public function getSubMessage(): string { return $this->subMessage; } - public function getSubCode(): ?string + public function getSubCode(): string { return $this->subCode; } diff --git a/tests/Factory/CategoryFactoryTest.php b/tests/Factory/CategoryFactoryTest.php index 46bf5f0..8e8d04f 100644 --- a/tests/Factory/CategoryFactoryTest.php +++ b/tests/Factory/CategoryFactoryTest.php @@ -282,7 +282,7 @@ public function createLevelAttributeDataProvider(): \Generator $expected->name = 'someName'; $expected->values = []; - yield ['no values' => $data, $expected]; + yield 'no values' => [$data, $expected]; $data = [ 'property_id' => '123', @@ -305,7 +305,7 @@ public function createLevelAttributeDataProvider(): \Generator $expected->name = 'someName'; $expected->values = [$levelValueNoLeaf, $levelValueIsLeaf]; - yield ['with values' => $data, $expected]; + yield 'with values' => [$data, $expected]; } /** @@ -330,7 +330,7 @@ public function createLevelAttributeValueDataProvider(): \Generator $expected->id = '1'; $expected->isLeaf = false; - yield ['no leaf' => $data, $expected]; + yield 'no leaf' => [$data, $expected]; $data = [ "id" => "1", @@ -343,6 +343,6 @@ public function createLevelAttributeValueDataProvider(): \Generator $expected->id = '1'; $expected->isLeaf = true; - yield ['is leaf' => $data, $expected]; + yield 'is leaf' => [$data, $expected]; } }