From 176df24e95b53baa700d5368bb8137e7d1064b9e Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Mon, 15 Jun 2020 15:49:28 -0400 Subject: [PATCH 1/3] Add responseBody property to ApiErrorException. --- src/Connector/Client.php | 17 ++++++------ src/Exception/ApiErrorException.php | 41 ++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/Connector/Client.php b/src/Connector/Client.php index bf795d4f..2fe86648 100644 --- a/src/Connector/Client.php +++ b/src/Connector/Client.php @@ -139,22 +139,21 @@ public function makeRequest(string $verb, string $path, array $options = []): Re public function processResponse(ResponseInterface $response) { - $body = $response->getBody(); - - $object = json_decode($body); + $body_json = $response->getBody(); + $body = json_decode($body_json); if (json_last_error() !== JSON_ERROR_NONE) { - return $body; + return $body_json; } - if (property_exists($object, '_embedded') && property_exists($object->_embedded, 'items')) { - return $object->_embedded->items; + if (property_exists($body, '_embedded') && property_exists($body->_embedded, 'items')) { + return $body->_embedded->items; } - if (property_exists($object, 'error') && property_exists($object, 'message')) { - throw new ApiErrorException($object); + if (property_exists($body, 'error') && property_exists($body, 'message')) { + throw new ApiErrorException($body); } - return $object; + return $body; } /** diff --git a/src/Exception/ApiErrorException.php b/src/Exception/ApiErrorException.php index 5a3ce46e..85ab9528 100644 --- a/src/Exception/ApiErrorException.php +++ b/src/Exception/ApiErrorException.php @@ -9,19 +9,26 @@ */ class ApiErrorException extends Exception { + + /** + * @var object + */ + private $response_body; + /** * ApiErrorException Constructor. * - * @param object $object + * @param object $response_body * @param string $message * @param int $code * @param Exception $previous */ - public function __construct($object, $message = "", $code = 0, Exception $previous = null) + public function __construct($response_body, $message = "", $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); - $this->setError($object); + $this->setResponseBody($response_body); + $this->setError($response_body); } /** @@ -33,21 +40,35 @@ public function __toString() } /** - * Sets the error. + * Sets message and code properties. * - * @param object $object + * @param object $response_body */ - public function setError($object) + public function setError($response_body) { - if (is_array($object->message) || is_object($object->message)) { + if (is_array($response_body->message) || is_object($response_body->message)) { $output = ''; - foreach ($object->message as $message) { + foreach ($response_body->message as $message) { $output .= $message . PHP_EOL; } $this->message = $output; } else { - $this->code = $object->error; - $this->message = $object->message; + $this->code = $response_body->error; + $this->message = $response_body->message; } } + + /** + * @return object + */ + public function getResponseBody() { + return $this->response_body; + } + + /** + * @param object $response_body + */ + private function setResponseBody($response_body) { + $this->response_body = $response_body; + } } From 649ac17e591c3b2cb955dce8b103ec0be1d6fc65 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Mon, 15 Jun 2020 15:50:40 -0400 Subject: [PATCH 2/3] mend --- src/Exception/ApiErrorException.php | 6 +++--- tests/Endpoints/ErrorTest.php | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Exception/ApiErrorException.php b/src/Exception/ApiErrorException.php index 85ab9528..da946834 100644 --- a/src/Exception/ApiErrorException.php +++ b/src/Exception/ApiErrorException.php @@ -13,7 +13,7 @@ class ApiErrorException extends Exception /** * @var object */ - private $response_body; + private $responseBody; /** * ApiErrorException Constructor. @@ -62,13 +62,13 @@ public function setError($response_body) * @return object */ public function getResponseBody() { - return $this->response_body; + return $this->responseBody; } /** * @param object $response_body */ private function setResponseBody($response_body) { - $this->response_body = $response_body; + $this->responseBody = $response_body; } } diff --git a/tests/Endpoints/ErrorTest.php b/tests/Endpoints/ErrorTest.php index dc51c454..456b5781 100644 --- a/tests/Endpoints/ErrorTest.php +++ b/tests/Endpoints/ErrorTest.php @@ -88,5 +88,6 @@ public function testApiErrorException() AcquiaCloudApi\Exception\ApiErrorException: [forbidden]: You do not have permission to view applications.\n EOM; $this->assertEquals($exception->__toString(), $errorMessage); + $this->assertEquals( $object, $exception->getResponseBody()); } } From 09017d7c6a2b60da7ba5974fde187a462d417097 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Mon, 15 Jun 2020 15:59:23 -0400 Subject: [PATCH 3/3] PHPCSGIT_SSH_COMMAND=ssh -o StrictHostKeyChecking=no --- src/Exception/ApiErrorException.php | 14 ++++++++------ tests/Endpoints/ErrorTest.php | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Exception/ApiErrorException.php b/src/Exception/ApiErrorException.php index da946834..c679de4f 100644 --- a/src/Exception/ApiErrorException.php +++ b/src/Exception/ApiErrorException.php @@ -61,14 +61,16 @@ public function setError($response_body) /** * @return object */ - public function getResponseBody() { - return $this->responseBody; - } + public function getResponseBody() + { + return $this->responseBody; + } /** * @param object $response_body */ - private function setResponseBody($response_body) { - $this->responseBody = $response_body; - } + private function setResponseBody($response_body) + { + $this->responseBody = $response_body; + } } diff --git a/tests/Endpoints/ErrorTest.php b/tests/Endpoints/ErrorTest.php index 456b5781..c9620ef4 100644 --- a/tests/Endpoints/ErrorTest.php +++ b/tests/Endpoints/ErrorTest.php @@ -88,6 +88,6 @@ public function testApiErrorException() AcquiaCloudApi\Exception\ApiErrorException: [forbidden]: You do not have permission to view applications.\n EOM; $this->assertEquals($exception->__toString(), $errorMessage); - $this->assertEquals( $object, $exception->getResponseBody()); + $this->assertEquals($object, $exception->getResponseBody()); } }