Skip to content

Commit

Permalink
refactor alibaba exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik-Czulak committed Feb 22, 2024
1 parent 12fcfdb commit a80b62c
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 32 deletions.
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
{
/**
* @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());
}
}

0 comments on commit a80b62c

Please sign in to comment.