Skip to content

Commit

Permalink
Merge pull request #32 from youcan-shop/YCP-987
Browse files Browse the repository at this point in the history
YCP-987: Improving exception handling
  • Loading branch information
y-abderrazik committed Jun 27, 2023
2 parents 12e04c2 + 40836c2 commit 5dab292
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/API/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use InvalidArgumentException;
use YouCan\Pay\API\APIServiceInterface;
use YouCan\Pay\API\Exceptions\Keys\UnsetPrivateKeyException;
use YouCan\Pay\API\Exceptions\Keys\UnsetPublicKeyException;

abstract class Endpoint
{
Expand Down Expand Up @@ -35,14 +37,14 @@ protected function createEndpoint(): string
protected function assertPrivateKeyIsSet(): void
{
if ($this->apiService->getPrivateKey() === null) {
throw new InvalidArgumentException("private key not set");
throw new UnsetPrivateKeyException("private key not set");
}
}

protected function assertPublicKeyIsSet(): void
{
if ($this->apiService->getPublicKey() === null) {
throw new InvalidArgumentException("public key not set");
throw new UnsetPublicKeyException("public key not set");
}
}
}
11 changes: 6 additions & 5 deletions src/API/Endpoints/KeysEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace YouCan\Pay\API\Endpoints;

use YouCan\Pay\API\Exceptions\InvalidResponseException;
use YouCan\Pay\API\Exceptions\ServerException;
use YouCan\Pay\API\Response;

class KeysEndpoint extends Endpoint
Expand All @@ -13,7 +14,7 @@ class KeysEndpoint extends Endpoint
* @param string|null $privateKey
* @param string|null $publicKey
* @return bool
* @throws InvalidResponseException
* @throws ServerException
*/
public function check(?string $privateKey = null, ?string $publicKey = null): bool
{
Expand All @@ -40,7 +41,7 @@ protected function endpoint(): string
/**
* @param Response $response
* @return bool
* @throws InvalidResponseException
* @throws ServerException
*/
private function assertResponse(Response $response): bool
{
Expand All @@ -49,10 +50,10 @@ private function assertResponse(Response $response): bool
}

if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
throw new InvalidResponseException(
$response->getStatusCode(),
throw new ServerException(
'internal error from server. Support has been notified. Please try again!',
json_encode($response->getResponse()),
'internal error from server. Support has been notified. Please try again!'
$response->getStatusCode(),
);
}

Expand Down
44 changes: 27 additions & 17 deletions src/API/Endpoints/TokenEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace YouCan\Pay\API\Endpoints;

use InvalidArgumentException;
use YouCan\Pay\API\Exceptions\InvalidResponseException;
use YouCan\Pay\API\Exceptions\ServerException;
use YouCan\Pay\API\Exceptions\Token\MissingTokenException;
use YouCan\Pay\API\Exceptions\UnexpectedResultException;
use YouCan\Pay\API\Exceptions\UnsupportedResponseException;
use YouCan\Pay\API\Exceptions\ValidationException;
use YouCan\Pay\API\Response;
use YouCan\Pay\Models\Token;
Expand Down Expand Up @@ -51,16 +53,16 @@ protected function endpoint(): string
/**
* @param Response $response
*
* @throws ValidationException|InvalidArgumentException
* @throws MissingTokenException|ValidationException|UnexpectedResultException|ServerException|UnsupportedResponseException
*/
private function assertResponse(Response $response): void
{
if ($response->getStatusCode() === 200) {
if (!is_array($response->get('token')) || !is_string($response->get('token')['id'])) {
throw new InvalidResponseException(
$response->getStatusCode(),
throw new MissingTokenException(
'missing token in response. Please try again or contact support',
json_encode($response->getResponse()),
'missing token in response. Please try again or contact support'
$response->getStatusCode(),
);
}

Expand All @@ -69,34 +71,42 @@ private function assertResponse(Response $response): void

if ($response->getStatusCode() === 404) {
if ($response->get('success') === false && is_string($response->get('message'))) {
throw new ValidationException((string)$response->get('message'));
throw new ValidationException(
(string)$response->get('message'),
json_encode($response->getResponse()),
$response->getStatusCode(),
);
}
}

if ($response->getStatusCode() === 422) {
if ($response->get('success') === false && is_string($response->get('message'))) {
throw new ValidationException((string)$response->get('message'));
throw new ValidationException(
(string)$response->get('message'),
json_encode($response->getResponse()),
$response->getStatusCode(),
);
}

throw new InvalidResponseException(
$response->getStatusCode(),
throw new UnexpectedResultException(
'got unexpected result from server. Validation response with wrong payload',
json_encode($response->getResponse()),
'got unexpected result from server. Validation response with wrong payload'
$response->getStatusCode()
);
}

if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
throw new InvalidResponseException(
$response->getStatusCode(),
throw new ServerException(
'internal error from server. Support has been notified. Please try again!',
json_encode($response->getResponse()),
'internal error from server. Support has been notified. Please try again!'
$response->getStatusCode(),
);
}

throw new InvalidResponseException(
$response->getStatusCode(),
throw new UnsupportedResponseException(
'not supported status code from the server.',
json_encode($response->getResponse()),
'not supported status code from the server.'
$response->getStatusCode(),
);
}
}
32 changes: 32 additions & 0 deletions src/API/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace YouCan\Pay\API\Exceptions;

use Throwable;

class BaseException extends \RuntimeException
{
/** @var string|null */
protected $response;

public function __construct(
string $message,
string $response = null,
int $code = 0,
Throwable $previous = null
) {
parent::__construct($message, $code, $previous);

$this->response = $response;
}

public function getResponse()
{
return $this->response;
}

public function hasResponse(): bool
{
return $this->response !== null;
}
}
10 changes: 10 additions & 0 deletions src/API/Exceptions/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace YouCan\Pay\API\Exceptions;

/**
* Exception when a client error is encountered (4xx codes)
*/
class ClientException extends BaseException
{
}
9 changes: 9 additions & 0 deletions src/API/Exceptions/Keys/UnsetPrivateKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace YouCan\Pay\API\Exceptions\Keys;

use YouCan\Pay\API\Exceptions\ValidationException;

class UnsetPrivateKeyException extends ValidationException
{
}
9 changes: 9 additions & 0 deletions src/API/Exceptions/Keys/UnsetPublicKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace YouCan\Pay\API\Exceptions\Keys;

use YouCan\Pay\API\Exceptions\ValidationException;

class UnsetPublicKeyException extends ValidationException
{
}
10 changes: 10 additions & 0 deletions src/API/Exceptions/ServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace YouCan\Pay\API\Exceptions;

/**
* Exception when a server error is encountered (5xx codes)
*/
class ServerException extends BaseException
{
}
9 changes: 9 additions & 0 deletions src/API/Exceptions/Token/MissingTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace YouCan\Pay\API\Exceptions\Token;

use YouCan\Pay\API\Exceptions\BaseException;

class MissingTokenException extends BaseException
{
}
7 changes: 7 additions & 0 deletions src/API/Exceptions/UnexpectedResultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace YouCan\Pay\API\Exceptions;

class UnexpectedResultException extends BaseException
{
}
7 changes: 7 additions & 0 deletions src/API/Exceptions/UnsupportedResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace YouCan\Pay\API\Exceptions;

class UnsupportedResponseException extends BaseException
{
}
8 changes: 1 addition & 7 deletions src/API/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

namespace YouCan\Pay\API\Exceptions;

use Exception;

class ValidationException extends Exception
class ValidationException extends ClientException
{
public function __construct(string $message)
{
parent::__construct($message);
}
}
5 changes: 2 additions & 3 deletions tests/API/Endpoints/TokenEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use Tests\API\FakeAPIService;
use Tests\BaseTestCase;
use YouCan\Pay\API\Endpoints\TokenEndpoint;
use YouCan\Pay\API\Endpoints\TransactionEndpoint;
use YouCan\Pay\API\Exceptions\InvalidResponseException;
use YouCan\Pay\API\Exceptions\ServerException;
use YouCan\Pay\API\Exceptions\ValidationException;
use YouCan\Pay\API\Response;

Expand Down Expand Up @@ -87,7 +86,7 @@ public function test_validation_exception()

public function test_internal_error()
{
$this->expectException(InvalidResponseException::class);
$this->expectException(ServerException::class);

$response = new Response(
500,
Expand Down

0 comments on commit 5dab292

Please sign in to comment.