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..c679de4f 100644 --- a/src/Exception/ApiErrorException.php +++ b/src/Exception/ApiErrorException.php @@ -9,19 +9,26 @@ */ class ApiErrorException extends Exception { + + /** + * @var object + */ + private $responseBody; + /** * 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,37 @@ 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->responseBody; + } + + /** + * @param object $response_body + */ + private function setResponseBody($response_body) + { + $this->responseBody = $response_body; + } } diff --git a/tests/Endpoints/ErrorTest.php b/tests/Endpoints/ErrorTest.php index dc51c454..c9620ef4 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()); } }