Skip to content

Commit 45cf42c

Browse files
committed
Proper exception handling for ReCodEx API requests.
1 parent ea41d93 commit 45cf42c

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

app/helpers/Recodex/RecodexApiHelper.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,42 @@ private function processJsonBody($response)
161161
return $body['payload'] ?? null;
162162
}
163163

164+
/**
165+
* Perform an HTTP request and return decoded JSON response.
166+
* @param string $method HTTP method
167+
* @param string $url suffix for the base URL
168+
* @param array $options for GuzzleHttp request
169+
* @return array|string|int|bool|null decoded JSON response
170+
* @throws RecodexApiException
171+
* @throws InvalidAccessTokenException
172+
*/
173+
private function request(string $method, string $url, array $options)
174+
{
175+
try {
176+
$response = $this->client->request($method, $url, $options);
177+
} catch (GuzzleHttp\Exception\ClientException $e) {
178+
if ($e->hasResponse()) {
179+
return $this->processJsonBody($e->getResponse());
180+
}
181+
throw new RecodexApiException("HTTP request to ReCodEx API failed: " . $e->getMessage(), $e);
182+
} catch (GuzzleHttp\Exception\GuzzleException $e) {
183+
throw new RecodexApiException("HTTP request to ReCodEx API failed: " . $e->getMessage(), $e);
184+
}
185+
return $this->processJsonBody($response);
186+
}
187+
164188
/**
165189
* Perform a GET request and return decoded JSON response.
166190
* @param string $url suffix for the base URL
167191
* @param array $params to be encoded in URL query
168192
* @param array $headers initial HTTP headers
169193
* @return array|string|int|bool|null decoded JSON response
194+
* @throws RecodexApiException
195+
* @throws InvalidAccessTokenException
170196
*/
171197
private function get(string $url, array $params = [], array $headers = [])
172198
{
173-
$response = $this->client->get($url, $this->prepareOptions($params, null, $headers));
174-
return $this->processJsonBody($response);
199+
return $this->request('GET', $url, $this->prepareOptions($params, null, $headers));
175200
}
176201

177202
/**
@@ -181,11 +206,12 @@ private function get(string $url, array $params = [], array $headers = [])
181206
* @param string|array|null $body (array is encoded as JSON)
182207
* @param array $headers initial HTTP headers
183208
* @return array|string|int|bool|null decoded JSON response
209+
* @throws RecodexApiException
210+
* @throws InvalidAccessTokenException
184211
*/
185212
private function post(string $url, array $params = [], $body = null, array $headers = [])
186213
{
187-
$response = $this->client->post($url, $this->prepareOptions($params, $body, $headers));
188-
return $this->processJsonBody($response);
214+
return $this->request('POST', $url, $this->prepareOptions($params, $body, $headers));
189215
}
190216

191217
/**
@@ -194,11 +220,12 @@ private function post(string $url, array $params = [], $body = null, array $head
194220
* @param array $params to be encoded in URL query
195221
* @param array $headers initial HTTP headers
196222
* @return array|string|int|bool|null decoded JSON response
223+
* @throws RecodexApiException
224+
* @throws InvalidAccessTokenException
197225
*/
198226
private function delete(string $url, array $params = [], array $headers = [])
199227
{
200-
$response = $this->client->delete($url, $this->prepareOptions($params, null, $headers));
201-
return $this->processJsonBody($response);
228+
return $this->request('DELETE', $url, $this->prepareOptions($params, null, $headers));
202229
}
203230

204231
/**

0 commit comments

Comments
 (0)