Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add category level attributes api support #3

Merged
merged 12 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'],
Expand Down
3 changes: 2 additions & 1 deletion src/Endpoint/CategoryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 32 additions & 0 deletions src/Exception/AlibabaApiError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Exception;

class AlibabaApiError extends AlibabaException
{
/**
* @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;
}
}
25 changes: 1 addition & 24 deletions src/Exception/AlibabaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,6 @@

namespace Kyto\Alibaba\Exception;

class AlibabaException extends \RuntimeException
abstract class AlibabaException extends \RuntimeException
Dominik-Czulak marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @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;
}
}
19 changes: 19 additions & 0 deletions src/Exception/UnexpectedApiResultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Exception;

class UnexpectedApiResultException extends AlibabaException
{
/**
* @internal
*/
public function __construct(
string $message,
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
}
}
4 changes: 2 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Kyto\Alibaba\Tests;

use Kyto\Alibaba\Client;
use Kyto\Alibaba\Exception\AlibabaException;
use Kyto\Alibaba\Exception\AlibabaApiError;
use Kyto\Alibaba\Util\Clock;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function testRequest(bool $isSuccess, array $responseData): void
->willReturn($response);

if (!$isSuccess) {
$this->expectException(AlibabaException::class);
$this->expectException(AlibabaApiError::class);
}

$actual = $this->client->request(['hello' => 'world', 'test' => 'data']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
Expand Down
24 changes: 24 additions & 0 deletions tests/Exception/UnexpectedApiResultExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Tests\Exception;

use Kyto\Alibaba\Exception\UnexpectedApiResultException;
use PHPUnit\Framework\TestCase;

class UnexpectedApiResultExceptionTest extends TestCase
{
public function testConstruct(): void
{
$message = 'Message';
$code = 1;
$previous = new \RuntimeException('Previous');

$exception = new UnexpectedApiResultException($message, $code, $previous);

self::assertSame('Message', $exception->getMessage());
self::assertSame($code, $exception->getCode());
self::assertSame($previous, $exception->getPrevious());
}
}
Loading