Exemplo n.º 1
0
 /**
  * Process response.
  *
  * @return array
  */
 protected function processResponse()
 {
     $parsedResponse = \json_decode($this->response->getBody(), true);
     // Test if return a valid JSON.
     if (JSON_ERROR_NONE !== json_last_error()) {
         $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Invalid JSON returned';
         throw new HttpClientException($message, $this->response->getCode(), $this->request, $this->response);
     }
     $this->lookForErrors($parsedResponse);
     return $parsedResponse;
 }
 /**
  * Look for errors in the request.
  *
  * @param array $parsedResponse Parsed body response.
  */
 protected function lookForErrors($parsedResponse)
 {
     // Test if return a valid JSON.
     if (null === $parsedResponse) {
         throw new HttpClientException('Invalid JSON returned', $this->response->getCode(), $this->request, $this->response);
     }
     // Any non-200/201/202 response code indicates an error.
     if (!\in_array($this->response->getCode(), ['200', '201', '202'])) {
         if (!empty($parsedResponse['errors'][0])) {
             $errorMessage = $parsedResponse['errors'][0]['message'];
             $errorCode = $parsedResponse['errors'][0]['code'];
         } else {
             $errorMessage = $parsedResponse['errors']['message'];
             $errorCode = $parsedResponse['errors']['code'];
         }
         throw new HttpClientException(\sprintf('Error: %s [%s]', $errorMessage, $errorCode), $this->response->getCode(), $this->request, $this->response);
     }
 }