1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Error; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\Psr7\str; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class responsible for building meaningful exceptions. For HTTP problems, it produces a {@see HttpError} |
14
|
|
|
* exception, and supplies a error message with reasonable defaults. For user input problems, it produces a |
15
|
|
|
* {@see UserInputError} exception. For both, the problem is described, a potential solution is offered and |
16
|
|
|
* a link to further information is included. |
17
|
|
|
* |
18
|
|
|
* @package OpenStack\Common\Error |
19
|
|
|
*/ |
20
|
|
|
class Builder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The default domain to use for further link documentation. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $docDomain = 'http://docs.php-opencloud.com/en/latest/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The HTTP client required to validate the further links. |
31
|
|
|
* |
32
|
|
|
* @var ClientInterface |
33
|
|
|
*/ |
34
|
|
|
private $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ClientInterface $client |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(ClientInterface $client = null) |
40
|
|
|
{ |
41
|
2 |
|
$this->client = $client ?: new Client(); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Internal method used when outputting headers in the error description. |
46
|
|
|
* |
47
|
|
|
* @param $name |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
4 |
|
private function header($name) |
52
|
|
|
{ |
53
|
4 |
|
return sprintf("%s\n%s\n", $name, str_repeat('~', strlen($name))); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Before outputting custom links, it is validated to ensure that the user is not |
58
|
|
|
* directed off to a broken link. If a 404 is detected, it is hidden. |
59
|
|
|
* |
60
|
|
|
* @param $link The proposed link |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
2 |
|
private function linkIsValid($link) |
65
|
|
|
{ |
66
|
2 |
|
$link = $this->docDomain . $link; |
67
|
|
|
|
68
|
|
|
try { |
69
|
2 |
|
$resp = $this->client->request('HEAD', $link); |
70
|
2 |
|
} catch (ClientException $e) { |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return $resp->getStatusCode() < 400; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Helper method responsible for constructing and returning {@see BadResponseError} exceptions. |
78
|
|
|
* |
79
|
|
|
* @param RequestInterface $request The faulty request |
80
|
|
|
* @param ResponseInterface $response The error-filled response |
81
|
|
|
* |
82
|
|
|
* @return BadResponseError |
83
|
|
|
*/ |
84
|
2 |
|
public function httpError(RequestInterface $request, ResponseInterface $response) |
85
|
|
|
{ |
86
|
2 |
|
$message = $this->header('HTTP Error'); |
87
|
|
|
|
88
|
2 |
|
$message .= sprintf("The remote server returned a \"%d %s\" error for the following transaction:\n\n", |
89
|
2 |
|
$response->getStatusCode(), $response->getReasonPhrase()); |
90
|
|
|
|
91
|
2 |
|
$message .= $this->header('Request'); |
92
|
2 |
|
$message .= trim(str($request)) . PHP_EOL . PHP_EOL; |
93
|
|
|
|
94
|
2 |
|
$message .= $this->header('Response'); |
95
|
2 |
|
$message .= trim(str($response)) . PHP_EOL . PHP_EOL; |
96
|
|
|
|
97
|
2 |
|
$message .= $this->header('Further information'); |
98
|
2 |
|
$message .= $this->getStatusCodeMessage($response->getStatusCode()); |
99
|
|
|
|
100
|
|
|
$message .= "Visit http://docs.php-opencloud.com/en/latest/http-codes for more information about debugging " |
101
|
2 |
|
. "HTTP status codes, or file a support issue on https://github.com/php-opencloud/openstack/issues."; |
102
|
|
|
|
103
|
2 |
|
$e = new BadResponseError($message); |
104
|
2 |
|
$e->setRequest($request); |
105
|
2 |
|
$e->setResponse($response); |
106
|
|
|
|
107
|
2 |
|
return $e; |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
private function getStatusCodeMessage($statusCode) |
111
|
|
|
{ |
112
|
|
|
$errors = [ |
113
|
2 |
|
400 => 'Please ensure that your input values are valid and well-formed. ', |
114
|
2 |
|
401 => 'Please ensure that your authentication credentials are valid. ', |
115
|
2 |
|
404 => "Please ensure that the resource you're trying to access actually exists. ", |
116
|
2 |
|
500 => 'Please try this operation again once you know the remote server is operational. ', |
117
|
2 |
|
]; |
118
|
|
|
|
119
|
2 |
|
return isset($errors[$statusCode]) ? $errors[$statusCode] : ''; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Helper method responsible for constructing and returning {@see UserInputError} exceptions. |
124
|
|
|
* |
125
|
|
|
* @param string $expectedType The type that was expected from the user |
126
|
|
|
* @param mixed $userValue The incorrect value the user actually provided |
127
|
|
|
* @param string|null $furtherLink A link to further information if necessary (optional). |
128
|
|
|
* |
129
|
|
|
* @return UserInputError |
130
|
|
|
*/ |
131
|
2 |
|
public function userInputError($expectedType, $userValue, $furtherLink = null) |
132
|
|
|
{ |
133
|
2 |
|
$message = $this->header('User Input Error'); |
134
|
|
|
|
135
|
2 |
|
$message .= sprintf("%s was expected, but the following value was passed in:\n\n%s\n", |
136
|
2 |
|
$expectedType, print_r($userValue, true)); |
137
|
|
|
|
138
|
2 |
|
$message .= "Please ensure that the value adheres to the expectation above. "; |
139
|
|
|
|
140
|
2 |
|
if ($furtherLink && $this->linkIsValid($furtherLink)) { |
|
|
|
|
141
|
1 |
|
$message .= sprintf("Visit %s for more information about input arguments. ", $this->docDomain . $furtherLink); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
2 |
|
$message .= 'If you run into trouble, please open a support issue on https://github.com/php-opencloud/openstack/issues.'; |
145
|
|
|
|
146
|
2 |
|
return new UserInputError($message); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|