Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 46 additions & 18 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -66,9 +67,9 @@ public function auth($token)
*
* @param string $method
* @param string $endpoint
* @param string $body
* @param string|null $body
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @return ResponseInterface
*/
public function request($method, $endpoint, $body = null, $headers = [])
Expand All @@ -93,20 +94,46 @@ public function request($method, $endpoint, $body = null, $headers = [])
$status = $e->getResponse()->getStatusCode();

if ($status == 404) {
throw new Exception\NotFoundException($e->getResponse());
throw new Exception\NotFoundException($e->getResponse(), $e->getCode(), $e);
}

if ($status == 400 || $status == 422) {
throw new Exception\ValidationException($e->getResponse());
throw new Exception\ValidationException($e->getResponse(), $e->getCode(), $e);
}

if ($status == 412) {
throw new Exception\PreconditionFailedException($e->getResponse());
throw new Exception\PreconditionFailedException($e->getResponse(), $e->getCode(), $e);
}

throw new Exception\ClientException($e->getResponse());
throw new Exception\ClientException($e->getResponse(), $e->getCode(), $e);
} catch (ServerException $e) {
throw new Exception\ServerException($e->getResponse());
throw new Exception\ServerException($e->getResponse(), $e->getCode(), $e);
} catch (GuzzleRequestException $e) {
if ($e->hasResponse()) {
$status = $e->getResponse()->getStatusCode();

if ($status == 404) {
throw new Exception\NotFoundException($e->getResponse(), $e->getCode(), $e);
}

if ($status == 400 || $status == 422) {
throw new Exception\ValidationException($e->getResponse(), $e->getCode(), $e);
}

if ($status == 412) {
throw new Exception\PreconditionFailedException($e->getResponse(), $e->getCode(), $e);
}

if ($status >= 500) {
throw new Exception\ServerException($e->getResponse(), $e->getCode(), $e);
}

throw new Exception\ClientException($e->getResponse(), $e->getCode(), $e);
}

throw new Exception\RequestException($e->getMessage(), $e->getCode(), $e);
} catch (GuzzleException $e) {
throw new Exception\RequestException($e->getMessage(), $e->getCode(), $e);
}

return $response;
Expand All @@ -116,9 +143,9 @@ public function request($method, $endpoint, $body = null, $headers = [])
* Convenience method for GET requests
*
* @param string $endpoint
* @param string $body
* @param string|null $body
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @return ResponseInterface
*/
public function get($endpoint, $body = null, $headers = [])
Expand All @@ -130,9 +157,9 @@ public function get($endpoint, $body = null, $headers = [])
* Convenience method for POST requests
*
* @param string $endpoint
* @param string $body
* @param string|null $body
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @return ResponseInterface
*/
public function post($endpoint, $body = null, $headers = [])
Expand All @@ -148,9 +175,9 @@ public function post($endpoint, $body = null, $headers = [])
* Convenience method for PUT requests
*
* @param string $endpoint
* @param string $body
* @param string|null $body
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @return ResponseInterface
*/
public function put($endpoint, $body = null, $headers = [])
Expand All @@ -166,9 +193,9 @@ public function put($endpoint, $body = null, $headers = [])
* Convenience method for PATCH requests
*
* @param string $endpoint
* @param string $body
* @param string|null $body
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @return ResponseInterface
*/
public function patch($endpoint, $body = null, $headers = [])
Expand All @@ -184,9 +211,9 @@ public function patch($endpoint, $body = null, $headers = [])
* Convenience method for DELETE requests
*
* @param string $endpoint
* @param string $body
* @param string|null $body
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @return ResponseInterface
*/
public function delete($endpoint, $body = null, $headers = [])
Expand All @@ -206,7 +233,8 @@ public function delete($endpoint, $body = null, $headers = [])
* @param int $perPage
* @param array $filters
* @param array $headers
* @throws GuzzleException
* @throws Exception\UKFastException
* @throws Exception\InvalidJsonException
* @return Page
*/
public function paginatedRequest($endpoint, $page, $perPage, $filters = [], $headers = [])
Expand Down
7 changes: 4 additions & 3 deletions src/Exception/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApiException extends UKFastException
*/
protected $response;

public function __construct($response)
public function __construct($response, $code = 0, $previous = null)
{
$response->getBody()->rewind();
$this->response = $response;
Expand All @@ -34,14 +34,15 @@ public function __construct($response)
$this->errors = $this->getApiGatewayErrorFromBody($body);
}

$message = '';
if (!empty($this->errors)) {
$message = $this->errors[0]->detail;
if (empty($message)) {
$message = $this->errors[0]->title;
}

$this->message = $message;
}

parent::__construct($message, $code, $previous);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/RequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace UKFast\SDK\Exception;

class RequestException extends UKFastException
{
}
Comment thread
Copilot marked this conversation as resolved.
4 changes: 2 additions & 2 deletions src/Exception/UKFastException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class UKFastException extends RuntimeException
{
public function __construct($message)
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message);
parent::__construct($message, $code, $previous);
}
}
29 changes: 28 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Middleware;
Expand All @@ -13,8 +14,9 @@
use UKFast\SDK\Exception\ApiException;
use UKFast\SDK\Exception\InvalidJsonException;
use UKFast\SDK\Exception\NotFoundException;
use UKFast\SDK\Exception\ValidationException;
use UKFast\SDK\Exception\PreconditionFailedException;
use UKFast\SDK\Exception\RequestException as UKFastRequestException;
use UKFast\SDK\Exception\ValidationException;
use UKFast\SDK\Page;

class ClientTest extends TestCase
Expand Down Expand Up @@ -236,6 +238,31 @@ public function throws_validation_exception($statusCode)
$client->paginatedRequest("/", 1, 10);
}

/**
* @test
*/
public function preserves_the_original_exception_when_request_fails()
{
$request = new \GuzzleHttp\Psr7\Request('GET', '/');
$originalException = new GuzzleRequestException('Connection failed', $request);
$mock = new MockHandler([$originalException]);
$handler = HandlerStack::create($mock);
$guzzle = new Guzzle(['handler' => $handler]);
$client = new Client($guzzle);

$this->expectException(UKFastRequestException::class);
$this->expectExceptionMessage('Connection failed');

try {
$client->request('GET', '/');
} catch (UKFastRequestException $e) {
$this->assertInstanceOf(GuzzleRequestException::class, $e->getPrevious());
$this->assertSame($originalException, $e->getPrevious());
$this->assertSame('Connection failed', $e->getPrevious()->getMessage());
throw $e;
}
}

/**
* @test
*/
Expand Down