diff --git a/src/Client.php b/src/Client.php index 0f54a1e7..edec3501 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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 = []) @@ -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; @@ -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 = []) @@ -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 = []) @@ -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 = []) @@ -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 = []) @@ -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 = []) @@ -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 = []) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 57ef0df1..0ef96002 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -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; @@ -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); } /** diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php new file mode 100644 index 00000000..48d2ce0f --- /dev/null +++ b/src/Exception/RequestException.php @@ -0,0 +1,7 @@ +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 */