4
* (c) Copyright 2014 Rackspace US, Inc.
6
* Licensed under the Apache License, Version 2.0 (the "License"); you may
7
* not use this file except in compliance with the License. You may obtain
8
* a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
* License for the specific language governing permissions and limitations
19
namespace OpenStack\Common\Transport\Exception;
21
use OpenStack\Common\Exception;
22
use OpenStack\Common\Transport\RequestInterface;
23
use OpenStack\Common\Transport\ResponseInterface;
26
* Base exception that is thrown for requests that result in a HTTP error.
28
class RequestException extends Exception
30
/** @var \OpenStack\Common\Transport\RequestInterface */
33
/** @var \OpenStack\Common\Transport\ResponseInterface */
37
* Construct this exception like any other, but also inject Request and
38
* Response objects in case the user needs them for debugging.
40
* @param string $errorMessage Human-readable explanation of error
41
* @param \OpenStack\Common\Transport\RequestInterface $request The failed request
42
* @param \OpenStack\Common\Transport\ResponseInterface $response The server's response
44
public function __construct($errorMessage, RequestInterface $request, ResponseInterface $response)
46
parent::__construct($errorMessage, $response->getStatusCode());
48
$this->request = $request;
49
$this->response = $response;
53
* Factory method that creates an appropriate Exception object based on the
54
* Response's status code. The message is constructed here also.
56
* @param \OpenStack\Common\Transport\RequestInterface $request The failed request
57
* @param \OpenStack\Common\Transport\ResponseInterface $response The API's response
60
public static function create(RequestInterface $request, ResponseInterface $response)
62
$label = 'A HTTP error occurred';
64
$status = $response->getStatusCode();
67
401 => 'UnauthorizedException',
68
403 => 'ForbiddenException',
69
404 => 'ResourceNotFoundException',
70
405 => 'MethodNotAllowedException',
71
409 => 'ConflictException',
72
411 => 'LengthRequiredException',
73
422 => 'UnprocessableEntityException',
74
500 => 'ServerException'
78
"%s\n[Status] %s (%s)\n[URL] %s\n[Message] %s\n", $label,
79
(string) $request->getUrl(),
80
$status, $response->getReasonPhrase(),
81
(string) $response->getBody()
84
// Find custom exception class or use default
85
$exceptionClass = isset($exceptions[$status])
86
? sprintf("%s\\%s", __NAMESPACE__, $exceptions[$status])
89
return new $exceptionClass($message, $request, $response);
93
* Returns the server response.
95
* @return \OpenStack\Common\Transport\ResponseInterface
97
public function getResponse()
99
return $this->response;
103
* Returns the request that caused error.
105
* @return \OpenStack\Common\Transport\RequestInterface
107
public function getRequest()
109
return $this->request;
b'\\ No newline at end of file'