You are here

private function RestResponse::handleJsonResponse in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Rest/RestResponse.php \Drupal\salesforce\Rest\RestResponse::handleJsonResponse()
  2. 8.3 src/Rest/RestResponse.php \Drupal\salesforce\Rest\RestResponse::handleJsonResponse()

Helper function to eliminate repetitive json parsing.

Return value

$this

Throws

\Drupal\salesforce\Rest\RestException

1 call to RestResponse::handleJsonResponse()
RestResponse::__construct in src/Rest/RestResponse.php
RestResponse constructor.

File

src/Rest/RestResponse.php, line 73

Class

RestResponse
Class RestResponse.

Namespace

Drupal\salesforce\Rest

Code

private function handleJsonResponse() {
  $this->data = '';
  $response_body = $this
    ->getBody()
    ->getContents();
  if (empty($response_body)) {
    return NULL;
  }

  // Allow any exceptions here to bubble up:
  try {
    $data = Json::decode($response_body);
  } catch (\Exception $e) {
    throw new RestException($this, $e
      ->getMessage(), $e
      ->getCode(), $e);
  }
  if (empty($data) || !is_array($data)) {
    throw new RestException($this, $this
      ->t('Invalid response'));
  }
  if (!empty($data['error'])) {
    throw new RestException($this, $data['error']);
  }
  if (!empty($data[0]) && count($data) == 1) {
    $data = $data[0];
  }
  if (!empty($data['error'])) {
    throw new RestException($this, $data['error']);
  }
  if (!empty($data['errorCode'])) {
    throw new RestException($this, $data['errorCode']);
  }
  $this->data = $data;
  return $this;
}