Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 587
Expand file tree
/
Copy pathGithubExceptionThrower.php
More file actions
Latest commit
174 lines (142 loc) · 7.57 KB
/
Copy pathGithubExceptionThrower.php
File metadata and controls
174 lines (142 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespaceGithub\HttpClient\Plugin;
useGithub\Exception\ApiLimitExceedException;
useGithub\Exception\ErrorException;
useGithub\Exception\RuntimeException;
useGithub\Exception\SsoRequiredException;
useGithub\Exception\TwoFactorAuthenticationRequiredException;
useGithub\Exception\ValidationFailedException;
useGithub\HttpClient\Message\ResponseMediator;
useHttp\Client\Common\Plugin;
useHttp\Promise\Promise;
usePsr\Http\Message\RequestInterface;
usePsr\Http\Message\ResponseInterface;
/**
* @author Joseph Bielawski <stloyd@gmail.com>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
finalclass GithubExceptionThrower implements Plugin
{
/**
* @return Promise
*/
publicfunctionhandleRequest(RequestInterface$request, callable$next, callable$first): Promise
{
return$next($request)->then(function (ResponseInterface$response) use ($request) {
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
$this->checkGraphqlErrors($response);
return$response;
}
// If error:
$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
if ((429 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
thrownewApiLimitExceedException($limit, $reset);
}
if ((401 === $response->getStatusCode()) && $response->hasHeader('X-GitHub-OTP') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-OTP'), 'required;')) {
$type = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-OTP'), 9);
thrownewTwoFactorAuthenticationRequiredException($type);
}
$content = ResponseMediator::getContent($response);
if (is_array($content) && isset($content['message'])) {
if (400 === $response->getStatusCode()) {
thrownewErrorException(sprintf('%s (%s)', $content['message'], $response->getReasonPhrase()), 400);
}
if (422 === $response->getStatusCode() && isset($content['errors'])) {
$errors = [];
foreach ((array) $content['errors'] as$error) {
switch ($error['code'] ?? null) {
case'missing':
$errors[] = sprintf('The %s %s does not exist, for resource "%s"', $error['field'], $error['value'], $error['resource']);
break;
case'missing_field':
$errors[] = sprintf('Field "%s" is missing, for resource "%s"', $error['field'], $error['resource']);
break;
case'invalid':
if (isset($error['message'])) {
$errors[] = sprintf('Field "%s" is invalid, for resource "%s": "%s"', $error['field'], $error['resource'], $error['message']);
} else {
$errors[] = sprintf('Field "%s" is invalid, for resource "%s"', $error['field'], $error['resource']);
}
break;
case'already_exists':
$errors[] = sprintf('Field "%s" already exists, for resource "%s"', $error['field'], $error['resource']);
break;
default:
if (is_string($error)) {
$errors[] = $error;
break;
}
if (isset($error['message'])) {
$errors[] = $error['message'];
}
break;
}
}
thrownewValidationFailedException(
$errors ? 'Validation Failed: '.implode(', ', $errors) : 'Validation Failed',
422
);
}
}
if (502 == $response->getStatusCode() && isset($content['errors']) && is_array($content['errors'])) {
$errors = [];
foreach ($content['errors'] as$error) {
if (isset($error['message'])) {
$errors[] = $error['message'];
}
}
thrownewRuntimeException(implode(', ', $errors), 502);
}
if ((403 === $response->getStatusCode()) && $response->hasHeader('X-GitHub-SSO') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 'required;')) {
// The header will look something like this:
// required; url=https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4
// So we strip out the first 14 characters, leaving only the URL.
// @see https://developer.github.com/v3/auth/#authenticating-for-saml-sso
$url = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 14);
thrownewSsoRequiredException($url);
}
$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
if ((403 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && isset($content['message']) && (0 === strpos($content['message'], 'API rate limit exceeded'))) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
thrownewApiLimitExceedException($limit, $reset);
}
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
if ((403 === $response->getStatusCode()) && 0 < $reset && isset($content['message']) && (0 === strpos($content['message'], 'You have exceeded a secondary rate limit'))) {
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
thrownewApiLimitExceedException($limit, $reset);
}
thrownewRuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
});
}
/**
* The graphql api doesn't return a 5xx http status for errors. Instead it returns a 200 with an error body.
*
* @throws RuntimeException
*/
privatefunctioncheckGraphqlErrors(ResponseInterface$response): void
{
if ($response->getStatusCode() !== 200) {
return;
}
$content = ResponseMediator::getContent($response);
if (!is_array($content)) {
return;
}
if (!isset($content['errors']) || !is_array($content['errors'])) {
return;
}
$errors = [];
foreach ($content['errors'] as$error) {
if (isset($error['message'])) {
$errors[] = $error['message'];
}
}
if (empty($errors)) {
return;
}
thrownewRuntimeException(implode(', ', $errors));
}
}