Ejemplo n.º 1
0
 /**
  * Factory method that creates an appropriate Exception object based on the
  * Response's status code. The message is constructed here also.
  *
  * @param \OpenStack\Common\Transport\RequestInterface  $request  The failed request
  * @param \OpenStack\Common\Transport\ResponseInterface $response The API's response
  * @return self
  */
 public static function create(RequestInterface $request, ResponseInterface $response)
 {
     $label = 'A HTTP error occurred';
     $status = $response->getStatusCode();
     $exceptions = [401 => 'UnauthorizedException', 403 => 'ForbiddenException', 404 => 'ResourceNotFoundException', 405 => 'MethodNotAllowedException', 409 => 'ConflictException', 411 => 'LengthRequiredException', 422 => 'UnprocessableEntityException', 500 => 'ServerException'];
     $message = sprintf("%s\n[Status] %s (%s)\n[URL] %s\n[Message] %s\n", $label, (string) $request->getUrl(), $status, $response->getReasonPhrase(), (string) $response->getBody());
     // Find custom exception class or use default
     $exceptionClass = isset($exceptions[$status]) ? sprintf("%s\\%s", __NAMESPACE__, $exceptions[$status]) : __CLASS__;
     return new $exceptionClass($message, $request, $response);
 }
Ejemplo n.º 2
0
 /**
  * @inheritDoc
  * @param \OpenStack\Common\Transport\RequestInterface $adapter
  * @return \OpenStack\Common\Transport\ResponseInterface
  * @throws \OpenStack\Common\Transport\Exception\RequestException
  * @throws \GuzzleHttp\Exception\RequestException
  */
 public function send(RequestInterface $adapter)
 {
     try {
         $guzzleResponse = $this->client->send($adapter->getMessage());
         return new ResponseAdapter($guzzleResponse);
     } catch (GuzzleRequestException $e) {
         // In order to satisfy {@see GuzzleHttp\ClientInterface}, Guzzle
         // wraps all exceptions in its own RequestException class. This is
         // not useful for our end-users, so we need to make sure our own
         // versions are returned (Guzzle buffers them).
         $previous = $e->getPrevious();
         if ($previous instanceof Exception\RequestException) {
             throw $previous;
         }
         throw $e;
     }
 }