Skip to content

Commit

Permalink
handle empty result, revert alibaba nullable error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik-Czulak committed Feb 20, 2024
1 parent 0fd792a commit 12fcfdb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
7 changes: 2 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
);
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Endpoint/CategoryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 5 additions & 8 deletions src/Exception/AlibabaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Factory/CategoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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];
}

/**
Expand All @@ -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",
Expand All @@ -343,6 +343,6 @@ public function createLevelAttributeValueDataProvider(): \Generator
$expected->id = '1';
$expected->isLeaf = true;

yield ['is leaf' => $data, $expected];
yield 'is leaf' => [$data, $expected];
}
}

0 comments on commit 12fcfdb

Please sign in to comment.