Skip to content

Commit

Permalink
Merge pull request #5 from everlutionsk/guzzle-errors
Browse files Browse the repository at this point in the history
Turns off Guzzle's default behaviour of throwing exceptions for 4xx and 5xx responses. This allows the SDK to select and throw its own exceptions.
  • Loading branch information
vanqard authored Jul 2, 2024
2 parents 31fd81b + dad62df commit 7eb2d66
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Citadel\Action\SessionResolveRequest;
use Citadel\Action\SessionRevokeBearerRequest;
use Citadel\Action\SessionRevokeRequest;
use Citadel\Exception\CitadelBaseException;
use Citadel\Exception\ExceptionFactory;
use Citadel\Exception\InternalServerErrorException;
use Citadel\Exception\MalformedPayloadException;
use Citadel\Exception\MalformedTimestampException;
use Citadel\Interface\CitadelReportedExceptionInterface;
Expand All @@ -25,7 +27,7 @@

class Client implements ClientInterface
{
private const string SDK_VERSION = '0.4.1-php';
private const string SDK_VERSION = '0.4.2-php';

private GuzzleClient $guzzleClient;

Expand Down Expand Up @@ -79,7 +81,9 @@ public function sessionResolveBearer(SessionResolveBearerRequest $request): Sess
}

/**
* @throws InternalServerErrorException
* @throws MalformedPayloadException
* @throws CitadelBaseException
*/
public function sessionRevokeBearer(SessionRevokeBearerRequest $request): SessionRevokeBearerResponse
{
Expand All @@ -89,7 +93,8 @@ public function sessionRevokeBearer(SessionRevokeBearerRequest $request): Sessio
}

/**
* @throws MalformedPayloadException|CitadelReportedExceptionInterface
* @throws InternalServerErrorException
* @throws MalformedPayloadException|CitadelReportedExceptionInterface|CitadelBaseException
*/
private function sendRequest(
string $action,
Expand All @@ -112,7 +117,8 @@ private function sendRequest(
'Content-Type' => 'application/json',
'x-sdk-version' => self::SDK_VERSION
],
'body' => $requestBody
'body' => $requestBody,
'http_errors' => false
]);

$statusCode = $response->getStatusCode();
Expand Down
1 change: 0 additions & 1 deletion src/Exception/CitadelBaseException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Citadel\Exception;

use Citadel\Interface\CitadelReportedExceptionInterface;
use Exception;
use Throwable;

Expand Down
7 changes: 7 additions & 0 deletions src/Exception/ExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ class ExceptionFactory

/**
* @throws CitadelReportedExceptionInterface
* @throws InternalServerErrorException
* @throws MalformedPayloadException|CitadelBaseException
*/
public static function generateExceptionFromResponse(ResponseInterface $response) :never
{
$body = $response->getBody()->getContents();
$statusCode = $response->getStatusCode();

if ($statusCode >= 500) {
throw new InternalServerErrorException("Citadel reported internal server error", $statusCode);
}

try {
$errorResponse = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
Expand Down
6 changes: 6 additions & 0 deletions src/Exception/InternalServerErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Citadel\Exception;

class InternalServerErrorException extends CitadelBaseException
{
}

0 comments on commit 7eb2d66

Please sign in to comment.