/** * 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); }
/** * Given a response object, populate this object. * * This parses the JSON data and parcels out the data to the appropriate * fields. * * @param \OpenStack\Common\Transport\ResponseInterface $response A response object. * * @return \OpenStack\Identity\v2\IdentityService $this for the current object so * it can be used in chaining. */ protected function handleResponse($response) { $json = $response->json(); $this->tokenDetails = $json['access']['token']; $this->userDetails = $json['access']['user']; $this->serviceCatalog = $json['access']['serviceCatalog']; return $this; }