Skip to content

Commit

Permalink
Set exception response (#75)
Browse files Browse the repository at this point in the history
* Add responseBody property to ApiErrorException.
  • Loading branch information
grasmash committed Jun 15, 2020
1 parent 23f5ef9 commit 598ec6f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
17 changes: 8 additions & 9 deletions src/Connector/Client.php
Expand Up @@ -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;
}

/**
Expand Down
43 changes: 33 additions & 10 deletions src/Exception/ApiErrorException.php
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions tests/Endpoints/ErrorTest.php
Expand Up @@ -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());
}
}

0 comments on commit 598ec6f

Please sign in to comment.