/**
  * Handles a bad response when attempting to validate
  *
  * @param BadResponseException $e Exception encountered
  *
  * @throws BadResponseException
  */
 private function handleBadResponse(BadResponseException $e)
 {
     // 404 errors mean the resource no longer exists, so remove from
     // cache, and prevent an additional request by throwing the exception
     if ($e->getResponse()->getStatusCode() == 404) {
         $this->storage->delete($e->getRequest());
         throw $e;
     }
 }
 /**
  * Handles a bad response when attempting to validate.
  *
  * If the resource no longer exists, then remove from the cache.
  *
  * @param BadResponseException $e Exception encountered
  *
  * @throws BadResponseException
  */
 private function handleBadResponse(BadResponseException $e)
 {
     if (isset(self::$gone[$e->getResponse()->getStatusCode()])) {
         $this->storage->delete($e->getRequest());
     }
     throw $e;
 }
 /**
  * @inheritdoc
  *
  * Modifies Guzzle error messages to add more detail, based on the response
  * body.
  */
 public static function create(RequestInterface $request, ResponseInterface $response = null, \Exception $previous = null)
 {
     $e = parent::create($request, $response, $previous);
     if ($response === null) {
         return $e;
     }
     $originalMessage = $e->getMessage();
     $message = $originalMessage;
     $responseInfoProperties = ['message', 'detail', 'error', 'error_description'];
     try {
         $response->getBody()->seek(0);
         $json = $response->json();
         foreach ($responseInfoProperties as $property) {
             if (!empty($json[$property])) {
                 $message .= " [{$property}] " . implode('; ', (array) $json[$property]);
             }
         }
     } catch (ParseException $parseException) {
         // Occasionally the response body may not be JSON.
         $response->getBody()->seek(0);
         $body = $response->getBody()->getContents();
         if ($body) {
             $message .= ' [extra] Non-JSON response body';
             $message .= ' [body] ' . $body;
         } else {
             $message .= ' [extra] Empty response body';
         }
     }
     // Re-create the exception to alter the message.
     if ($message !== $originalMessage) {
         $className = get_class($e);
         $e = new $className($message, $e->getRequest(), $e->getResponse());
     }
     return $e;
 }
Example #4
0
 /**
  * @param \GuzzleHttp\Exception\BadResponseException $e
  * @return StorageApiEvent
  */
 public function responseError(\GuzzleHttp\Exception\BadResponseException $e)
 {
     $event = $this->prepareEvent();
     $event->setMessage(sprintf(self::MESSAGE_END, $this->task->getRunUrl()))->setDescription($e->getMessage())->setType(StorageApiEvent::TYPE_WARN);
     $this->save($event);
     return $event;
 }
Example #5
0
 /**
  * The standard error message from guzzle is quite poor in informations,
  * this will give little bit more sense to it and return it
  *
  * @param BadResponseException $e
  * @return BadResponseException
  */
 protected function handleError(BadResponseException $e)
 {
     $request = $e->getRequest();
     $response = $e->getResponse();
     $level = floor($response->getStatusCode() / 100);
     if ($level == '4') {
         $label = 'Client error response';
     } elseif ($level == '5') {
         $label = 'Server error response';
     } else {
         $label = 'Unsuccessful response';
     }
     $message = $label . ' [url] ' . $request->getUrl() . ' [status code] ' . $response->getStatusCode() . ' [message] ';
     try {
         $message .= $response->json()['message'];
     } catch (ParseException $e) {
         $message .= (string) $response->getBody();
     }
     return new BadResponseException($message, $request, $response, $e->getPrevious());
 }
Example #6
0
 /**
  * The standard error message from guzzle is quite poor in informations,
  * this will give little bit more sense to it and return it
  *
  * @param BadResponseException $e
  * @return BadResponseException
  */
 protected function handleError(BadResponseException $e)
 {
     $request = $e->getRequest();
     $response = $e->getResponse();
     $level = floor($response->getStatusCode() / 100);
     if ($level == '4') {
         $label = 'Client error response';
     } elseif ($level == '5') {
         $label = 'Server error response';
     } else {
         $label = 'Unsuccessful response';
     }
     $message = $label . ' [url] ' . $request->getUri() . ' [status code] ' . $response->getStatusCode() . ' [message] ';
     $body = $response->getBody();
     $json = json_decode($body, true);
     $message .= $json != null && array_key_exists('message', $json) ? $json['message'] : $body;
     if ($level == '4' && strpos($message, "page with this title already exists") !== false) {
         return new DuplicateTitleException($message, 0, $e->getPrevious());
     }
     return new BadResponseException($message, $request, $response, $e->getPrevious());
 }
Example #7
0
 /**
  * Handles the error for us, just a bit of code abstraction.
  *
  * @param BadResponseException $exception
  *
  * @throws JSONError
  */
 public static function handleError(BadResponseException $exception)
 {
     $response_body = (string) $exception->getResponse()->getBody();
     $array_body = decode($response_body);
     $code = $exception->getResponse()->getStatusCode();
     $message = null;
     if (isset($array_body['message'])) {
         $message = $array_body['message'];
     } elseif (isset($array_body['code'])) {
         $message = $array_body['code'];
     }
     throw new JSONError($message, $code, $array_body);
 }
Example #8
0
 /**
  * @param BadResponseException $ex
  * @param array $response
  *
  * @throws AuthenticationException
  * @throws InvalidDocumentStructureException
  * @throws ResourceNotFoundException
  * @throws VersionNotFoundException
  */
 private function handleInvalidApiUsage(BadResponseException $ex, array $response)
 {
     switch ($response['error']['errors'][0]['reason']) {
         case 'resource_not_found':
             throw new ResourceNotFoundException(sprintf('Resource not found at %s', $ex->getRequest()->getRequestTarget()), $ex->getCode());
         case 'version_not_found':
             throw new VersionNotFoundException('Version not found', $ex->getCode());
         case 'invalid_document_structure':
             throw new InvalidDocumentStructureException($response['error']['message'], $ex->getCode());
         case 'access_token_not_found':
         case 'insufficient_permissions':
             throw new AuthenticationException($response['error']['message'], $ex->getCode());
     }
 }
Example #9
0
 /**
  * @param ResponseInterface $response
  * @param string            $key
  *
  * @throws RepresentationErrorException on 422 response
  * @throws ClientException on 4xx errors
  * @throws ServerException on 5xx errors
  * @throws BadResponseException when key is not found in response data
  * @return array|bool
  */
 private function processResponse(ResponseInterface $response, $key = null)
 {
     $status = $response->getStatusCode();
     if (204 == $status) {
         return true;
     }
     $decoded = $response->json();
     $decoded = Convert::objectToArray($decoded);
     if (422 == $status) {
         //@codeCoverageIgnoreStart
         throw new RepresentationErrorException('', $this->client->getLastRequest(), $response);
         //@codeCoverageIgnoreEnd
     }
     if (200 <= $status && 300 > $status) {
         $this->lastResponse = $response;
         if (null === $key) {
             return $decoded;
         }
         if (false === array_key_exists($key, $decoded)) {
             //@codeCoverageIgnoreStart
             $message = sprintf('Key "%s" not found in data', $key);
             throw new BadResponseException($message, $this->client->getLastRequest(), $response);
             //@codeCoverageIgnoreEnd
         }
         return $decoded[$key];
     }
     //@codeCoverageIgnoreStart
     throw BadResponseException::create($this->client->getLastRequest(), $response);
     //@codeCoverageIgnoreEnd
 }
Example #10
0
 /**
  * @author WN
  * @param Exception\BadResponseException $e
  * @return array
  */
 private function formatBadResponseException(Exception\BadResponseException $e)
 {
     return ['message' => $e->getMessage(), 'request' => ['headers' => $e->getRequest()->getHeaders(), 'body' => $e->getRequest()->getBody()->getContents(), 'method' => $e->getRequest()->getMethod(), 'uri' => $e->getRequest()->getUri()], 'response' => ['body' => $e->getResponse() ? $e->getResponse()->getBody()->getContents() : '[EMPTY]', 'headers' => $e->getResponse() ? $e->getResponse()->getHeaders() : '[EMPTY]']];
 }
 private function remoteAppDoesNotRespond()
 {
     $this->response->shouldReceive('getBody')->andReturn($this->faker->word);
     $this->badResponseException->shouldReceive('getResponse')->andReturn($this->response);
     $this->client->shouldReceive('request')->andThrow($this->badResponseException);
 }
Example #12
0
 /**
  * Handle a bad response coming back when getting token credentials.
  *
  * @param BadResponseException $e
  *
  * @throws CredentialsException
  */
 protected function handleTokenCredentialsBadResponse(BadResponseException $e)
 {
     $response = $e->getResponse();
     $body = $response->getBody();
     $statusCode = $response->getStatusCode();
     throw new CredentialsException("Received HTTP status code [{$statusCode}] with message \"{$body}\" when getting token credentials.");
 }
Example #13
0
 /**
  * @param BadResponseException $exception
  * @param Pin $pin
  *
  * @throws InvalidTimelineToken
  * @throws Exception
  */
 private function handleException(BadResponseException $exception, Pin $pin)
 {
     switch ($exception->getCode()) {
         case 400:
             // Invalid pin object
             throw new InvalidPinObject($pin);
         case 403:
             // Invalid API key
             throw new InvalidTimelineToken($this->token);
         case 410:
             // Invalid user token
             throw new InvalidTimelineToken($this->token);
         case 429:
             // Rate-limit exceeded
             throw new RateLimitExceeded();
         case 503:
             // Service unavailable
             throw new ServiceUnavailable();
     }
     throw new Exception("Invalid API call, reason unknown");
 }
Example #14
0
 /**
  * @param BadResponseException $e
  * @return mixed
  */
 public static function getBadResponseResult(BadResponseException $e)
 {
     return $e->getResponse()->json();
 }
 /**
  * @param BadResponseException $e
  *
  * @return ApiException
  */
 public static function fromBadResponseException(BadResponseException $e)
 {
     $message = 'An error has occurred during the API call';
     if ($e->getResponse() instanceof ResponseInterface) {
         $body = (string) $e->getResponse()->getBody();
         $json = json_decode($body, true);
         if (isset($json['message'])) {
             $message = $json['message'];
         }
     }
     return new self($message);
 }