Skip to content

Commit

Permalink
Merge pull request #12 from Surfoo/develop
Browse files Browse the repository at this point in the history
Better exception management
  • Loading branch information
Surfoo authored Aug 6, 2021
2 parents 7b6e224 + 69c0d89 commit 4a33330
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
52 changes: 47 additions & 5 deletions src/Lib/Adapters/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Geocaching\Exception\GeocachingSdkException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;

class GuzzleHttpClient implements HttpClientInterface
{
Expand Down Expand Up @@ -105,7 +106,17 @@ public function get(string $uri, array $query = [], array $options = [])

try {
$this->response = $this->client->get($uri, $options);
} catch (TransferException $e) {
} catch (ConnectException $e) {
throw new GeocachingSdkException(
$e->getMessage(),
$e->getCode(),
[
'uri' => $uri,
'query' => $query,
'options' => $options,
]
);
} catch (RequestException $e) {
$response = null;
if ($e->hasResponse()) {
$response = json_decode($e->getResponse()->getBody());
Expand Down Expand Up @@ -142,7 +153,18 @@ public function post(string $uri, array $body = [], array $query = [], array $op

try {
$this->response = $this->client->post($uri, $options);
} catch (TransferException $e) {
} catch (ConnectException $e) {
throw new GeocachingSdkException(
$e->getMessage(),
$e->getCode(),
[
'uri' => $uri,
'body' => $body,
'query' => $query,
'options' => $options,
]
);
} catch (RequestException $e) {
$response = null;
if ($e->hasResponse()) {
$response = json_decode($e->getResponse()->getBody());
Expand Down Expand Up @@ -180,7 +202,18 @@ public function put(string $uri, array $body, array $query = [], array $options

try {
$this->response = $this->client->put($uri, $options);
} catch (TransferException $e) {
} catch (ConnectException $e) {
throw new GeocachingSdkException(
$e->getMessage(),
$e->getCode(),
[
'uri' => $uri,
'body' => $body,
'query' => $query,
'options' => $options,
]
);
} catch (RequestException $e) {
$response = null;
if ($e->hasResponse()) {
$response = json_decode($e->getResponse()->getBody());
Expand Down Expand Up @@ -210,7 +243,16 @@ public function delete(string $uri, array $options = [])

try {
$this->response = $this->client->delete($uri, $options);
} catch (TransferException $e) {
} catch (ConnectException $e) {
throw new GeocachingSdkException(
$e->getMessage(),
$e->getCode(),
[
'uri' => $uri,
'options' => $options,
]
);
} catch (RequestException $e) {
$response = null;
if ($e->hasResponse()) {
$response = json_decode($e->getResponse()->getBody());
Expand Down
4 changes: 2 additions & 2 deletions src/Sdk/GeocachingSdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function setGeocacheLog(array $geocacheLog, array $query = [], array $opt
/**
* swagger: DELETE /v{api-version}/geocachelogs/{referenceCode}/upvotes/{upvoteTypeId}
*
* @see https://api.groundspeak.com/documentation#delete-geocachelog-upvotes
* @see https://api.groundspeak.com/documentation#delete-geocachelog-upvote
* @see https://api.groundspeak.com/api-docs/index#!/GeocacheLogs/GeocacheLogs_DeleteUpvote
*
* @return \Geocaching\Lib\Adapters\GuzzleHttpClient
Expand All @@ -241,7 +241,7 @@ public function deleteGeocacheLogUpvotes(string $referenceCode, int $upvoteTypeI
/**
* swagger: POST /v{api-version}/geocachelogs/{referenceCode}/upvotes/{upvoteTypeId}
*
* @see https://api.groundspeak.com/documentation#create-geocachelog-upvotes
* @see https://api.groundspeak.com/documentation#add-geocachelog-upvote
* @see https://api.groundspeak.com/api-docs/index#!/GeocacheLogs/GeocacheLogs_AddUpvote
*
* @return \Geocaching\Lib\Adapters\GuzzleHttpClient
Expand Down
4 changes: 2 additions & 2 deletions src/Sdk/GeocachingSdkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ public function setGeocacheLogImages(string $referenceCode, array $imageToUpload
public function setGeocacheLog(array $geocacheLog, array $query = [], array $options = []);

/**
* @see https://api.groundspeak.com/documentation#delete-geocachelog-upvotes
* @see https://api.groundspeak.com/documentation#delete-geocachelog-upvote
*
* @return \Geocaching\Lib\Adapters\GuzzleHttpClient
*/
public function deleteGeocacheLogUpvotes(string $referenceCode, int $upvoteTypeId, array $options = []);

/**
* @see https://api.groundspeak.com/documentation#create-geocachelog-upvotes
* @see https://api.groundspeak.com/documentation#add-geocachelog-upvote
*
* @return \Geocaching\Lib\Adapters\GuzzleHttpClient
*/
Expand Down

0 comments on commit 4a33330

Please sign in to comment.