Skip to content

Commit

Permalink
feat: adds exceptions to discriminate client, server and network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioturdo committed May 5, 2023
1 parent 3fb0bf0 commit ec23718
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/ClientAdapter/ClientAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ImmobiliareLabs\BrazeSDK\ClientAdapter;

use ImmobiliareLabs\BrazeSDK\ClientResolvedResponse;
use ImmobiliareLabs\BrazeSDK\Exception\ClientException;
use ImmobiliareLabs\BrazeSDK\Exception\ServerException;
use ImmobiliareLabs\BrazeSDK\Exception\TransportException;
use ImmobiliareLabs\BrazeSDK\Request\BaseRequest;

Expand All @@ -18,13 +20,17 @@ public function setHeaders(array $headers): void;
* @return mixed the real response or a reference to it that will allow you to solve it later, for example a promise
*
* @throws TransportException
* @throws ServerException
* @throws ClientException
*/
public function makeRequest(string $method, string $path, ?string $body = null);

/**
* @param mixed $httpResponse the real response or a reference to it. It's the value returned by the makeRequest method
*
* @throws TransportException
* @throws ServerException
* @throws ClientException
*/
public function resolveResponse($httpResponse): ClientResolvedResponse;
}
9 changes: 6 additions & 3 deletions src/ClientAdapter/Psr18Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace ImmobiliareLabs\BrazeSDK\ClientAdapter;

use ImmobiliareLabs\BrazeSDK\ClientResolvedResponse;
use ImmobiliareLabs\BrazeSDK\Exception\ClientException;
use ImmobiliareLabs\BrazeSDK\Exception\NotValidBaseURIException;
use ImmobiliareLabs\BrazeSDK\Exception\TransportException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand Down Expand Up @@ -82,9 +84,10 @@ public function makeRequest(string $method, string $path, ?string $body = null):

try {
return $this->client->sendRequest($request);
} catch (ClientExceptionInterface $e) {
// is this correct?
throw new TransportException($e->getMessage(), 0, $e);
} catch (NetworkExceptionInterface $networkException) {
throw new TransportException($networkException->getMessage(), 0, $networkException);
} catch (ClientExceptionInterface $clientException) {
throw new ClientException($clientException->getMessage(), 0, $clientException);
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/ClientAdapter/SymfonyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ImmobiliareLabs\BrazeSDK\ClientAdapter;

use ImmobiliareLabs\BrazeSDK\ClientResolvedResponse;
use ImmobiliareLabs\BrazeSDK\Exception\ClientException;
use ImmobiliareLabs\BrazeSDK\Exception\ServerException;
use ImmobiliareLabs\BrazeSDK\Exception\TransportException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
Expand Down Expand Up @@ -37,9 +39,6 @@ public function setHeaders(array $headers): void
$this->headers = $headers;
}

/**
* @throws TransportException
*/
public function makeRequest(string $method, string $path, ?string $body = null): ResponseInterface
{
$options = [
Expand All @@ -57,15 +56,17 @@ public function makeRequest(string $method, string $path, ?string $body = null):

/**
* @param ResponseInterface $httpResponse
* @throws TransportException
*/
public function resolveResponse($httpResponse): ClientResolvedResponse
{
try {
return new ClientResolvedResponse($httpResponse->getStatusCode(), $httpResponse->getContent(false));
} catch (TransportExceptionInterface|ClientExceptionInterface|ServerExceptionInterface|RedirectionExceptionInterface $e) {
// ClientExceptionInterface, ServerExceptionInterface and RedirectionExceptionInterface never occurs because throw parameter is set to false
return new ClientResolvedResponse($httpResponse->getStatusCode(), $httpResponse->getContent());
} catch (TransportExceptionInterface $e) {
throw new TransportException($e->getMessage(), 0, $e);
} catch (ServerExceptionInterface $serverException) {
throw new ServerException($serverException->getMessage(), 0, $serverException);
} catch (ClientExceptionInterface|RedirectionExceptionInterface $clientException) {
throw new ClientException($clientException->getMessage(), 0, $clientException);
}
}
}
8 changes: 8 additions & 0 deletions src/Exception/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Exception;

class ClientException extends \RuntimeException
{

}
8 changes: 8 additions & 0 deletions src/Exception/ServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ImmobiliareLabs\BrazeSDK\Exception;

class ServerException extends \RuntimeException
{

}
3 changes: 2 additions & 1 deletion tests/ClientAdapter/Psr18AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ImmobiliareLabs\BrazeSDK\Test\ClientAdapter;

use ImmobiliareLabs\BrazeSDK\ClientAdapter\Psr18Adapter;
use ImmobiliareLabs\BrazeSDK\Exception\ClientException;
use ImmobiliareLabs\BrazeSDK\Exception\NotValidBaseURIException;
use ImmobiliareLabs\BrazeSDK\Exception\TransportException;
use ImmobiliareLabs\BrazeSDK\Region;
Expand Down Expand Up @@ -105,7 +106,7 @@ public function testMakeRequest(): void

public function testMakeRequestWithClientException(): void
{
$this->expectException(TransportException::class);
$this->expectException(ClientException::class);

$exception = $this->createMock(ClientExceptionInterface::class);

Expand Down

0 comments on commit ec23718

Please sign in to comment.