diff options
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Error')
5 files changed, 255 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Error/BadResponseError.php b/server/vendor/php-opencloud/common/src/Common/Error/BadResponseError.php new file mode 100644 index 0000000..f7640ea --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Error/BadResponseError.php @@ -0,0 +1,40 @@ +<?php + +namespace OpenCloud\Common\Error; + +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; + +/** + * Represents a HTTP-specific error, caused by 4xx or 5xx response statuses. + * + * @package OpenCloud\Common\Error + */ +class BadResponseError extends BaseError +{ + /** @var RequestInterface */ + private $request; + + /** @var ResponseInterface */ + private $response; + + public function setRequest(RequestInterface $request) + { + $this->request = $request; + } + + public function setResponse(ResponseInterface $response) + { + $this->response = $response; + } + + public function getRequest() + { + return $this->request; + } + + public function getResponse() + { + return $this->response; + } +} diff --git a/server/vendor/php-opencloud/common/src/Common/Error/BaseError.php b/server/vendor/php-opencloud/common/src/Common/Error/BaseError.php new file mode 100644 index 0000000..a7cb26e --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Error/BaseError.php @@ -0,0 +1,12 @@ +<?php + +namespace OpenCloud\Common\Error; + +/** + * Base error class. + * + * @package OpenCloud\Common\Error + */ +class BaseError extends \Exception +{ +} diff --git a/server/vendor/php-opencloud/common/src/Common/Error/Builder.php b/server/vendor/php-opencloud/common/src/Common/Error/Builder.php new file mode 100644 index 0000000..e3ccdfe --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Error/Builder.php @@ -0,0 +1,179 @@ +<?php + +namespace OpenCloud\Common\Error; + +use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; +use GuzzleHttp\Exception\ClientException; +use Psr\Http\Message\MessageInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; + +/** + * Class responsible for building meaningful exceptions. For HTTP problems, it produces a {@see HttpError} + * exception, and supplies a error message with reasonable defaults. For user input problems, it produces a + * {@see UserInputError} exception. For both, the problem is described, a potential solution is offered and + * a link to further information is included. + * + * @package OpenCloud\Common\Error + */ +class Builder +{ + /** + * The default domain to use for further link documentation. + * + * @var string + */ + private $docDomain = 'http://docs.php-opencloud.com/en/latest/'; + + /** + * The HTTP client required to validate the further links. + * + * @var ClientInterface + */ + private $client; + + /** + * @param ClientInterface $client + */ + public function __construct(ClientInterface $client = null) + { + $this->client = $client ?: new Client(); + } + + /** + * Internal method used when outputting headers in the error description. + * + * @param $name + * + * @return string + */ + private function header($name) + { + return sprintf("%s\n%s\n", $name, str_repeat('~', strlen($name))); + } + + /** + * Before outputting custom links, it is validated to ensure that the user is not + * directed off to a broken link. If a 404 is detected, it is hidden. + * + * @param $link The proposed link + * + * @return bool + */ + private function linkIsValid($link) + { + $link = $this->docDomain . $link; + + try { + return $this->client->request('HEAD', $link)->getStatusCode() < 400; + } catch (ClientException $e) { + return false; + } + } + + /** + * @param MessageInterface $message + * + * @codeCoverageIgnore + * @return string + */ + public function str(MessageInterface $message) + { + if ($message instanceof RequestInterface) { + $msg = trim($message->getMethod() . ' ' + . $message->getRequestTarget()) + . ' HTTP/' . $message->getProtocolVersion(); + if (!$message->hasHeader('host')) { + $msg .= "\r\nHost: " . $message->getUri()->getHost(); + } + } elseif ($message instanceof ResponseInterface) { + $msg = 'HTTP/' . $message->getProtocolVersion() . ' ' + . $message->getStatusCode() . ' ' + . $message->getReasonPhrase(); + } + + foreach ($message->getHeaders() as $name => $values) { + $msg .= "\r\n{$name}: " . implode(', ', $values); + } + + if ($message->getBody()->getSize() < ini_get('memory_limit')) { + $msg .= "\r\n\r\n" . $message->getBody(); + } + + return $msg; + } + + /** + * Helper method responsible for constructing and returning {@see BadResponseError} exceptions. + * + * @param RequestInterface $request The faulty request + * @param ResponseInterface $response The error-filled response + * + * @return BadResponseError + */ + public function httpError(RequestInterface $request, ResponseInterface $response) + { + $message = $this->header('HTTP Error'); + + $message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", + $response->getStatusCode(), $response->getReasonPhrase()); + + $message .= $this->header('Request'); + $message .= trim($this->str($request)) . PHP_EOL . PHP_EOL; + + $message .= $this->header('Response'); + $message .= trim($this->str($response)) . PHP_EOL . PHP_EOL; + + $message .= $this->header('Further information'); + $message .= $this->getStatusCodeMessage($response->getStatusCode()); + + $message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " + . "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues."; + + $e = new BadResponseError($message); + $e->setRequest($request); + $e->setResponse($response); + + return $e; + } + + private function getStatusCodeMessage($statusCode) + { + $errors = [ + 400 => 'Please ensure that your input values are valid and well-formed. ', + 401 => 'Please ensure that your authentication credentials are valid. ', + 404 => "Please ensure that the resource you're trying to access actually exists. ", + 500 => 'Please try this operation again once you know the remote server is operational. ', + ]; + + return isset($errors[$statusCode]) ? $errors[$statusCode] : ''; + } + + /** + * Helper method responsible for constructing and returning {@see UserInputError} exceptions. + * + * @param string $expectedType The type that was expected from the user + * @param mixed $userValue The incorrect value the user actually provided + * @param string|null $furtherLink A link to further information if necessary (optional). + * + * @return UserInputError + */ + public function userInputError($expectedType, $userValue, $furtherLink = null) + { + $message = $this->header('User Input Error'); + + $message .= sprintf("%s was expected, but the following value was passed in:\n\n%s\n", + $expectedType, print_r($userValue, true)); + + $message .= "Please ensure that the value adheres to the expectation above. "; + + if ($furtherLink && $this->linkIsValid($furtherLink)) { + $message .= sprintf("Visit %s for more information about input arguments. ", $this->docDomain . $furtherLink); + } + + $message .= 'If you run into trouble, please open a support issue on https://github.com/php-opencloud/openstack/issues.'; + + return new UserInputError($message); + } +} diff --git a/server/vendor/php-opencloud/common/src/Common/Error/NotImplementedError.php b/server/vendor/php-opencloud/common/src/Common/Error/NotImplementedError.php new file mode 100644 index 0000000..3e01d74 --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Error/NotImplementedError.php @@ -0,0 +1,12 @@ +<?php + +namespace OpenCloud\Common\Error; + +/** + * Error to indicate functionality which has not been implemented yet. + * + * @package OpenCloud\Common\Error + */ +class NotImplementedError extends BaseError +{ +} diff --git a/server/vendor/php-opencloud/common/src/Common/Error/UserInputError.php b/server/vendor/php-opencloud/common/src/Common/Error/UserInputError.php new file mode 100644 index 0000000..964875e --- /dev/null +++ b/server/vendor/php-opencloud/common/src/Common/Error/UserInputError.php @@ -0,0 +1,12 @@ +<?php + +namespace OpenCloud\Common\Error; + +/** + * Represents a user input error, caused by an incorrect type or malformed value. + * + * @package OpenCloud\Common\Error + */ +class UserInputError extends BaseError +{ +} |
