Skip to content

Commit

Permalink
Fix exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-tekiela committed Jul 29, 2021
1 parent 4c57559 commit c4c82ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Exceptions/EmptyResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Exceptions;

use Exception;

class EmptyResponseException extends Exception
{
}
10 changes: 6 additions & 4 deletions src/StackAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dharman;

use Exceptions\EmptyResponseException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Psr7\Message;
Expand Down Expand Up @@ -44,12 +45,13 @@ public function request(string $method, string $url, array $args): \stdClass {
}
} catch (RequestException $e) {
$response = $e->getResponse();
if (isset($response)) {
if (($json = json_decode($response->getBody()->getContents())) && isset($json->error_id) && $json->error_id == 502) {
if ($response) {
$jsonResponse = json_decode((string) $response->getBody());
if ($jsonResponse->error_id == 502) {
sleep(10 * 60);
return $this->request($method, $url, $args);
} else {
throw new \Exception(Message::toString($e->getResponse()));
throw $e;
}
} else {
throw $e;
Expand All @@ -59,7 +61,7 @@ public function request(string $method, string $url, array $args): \stdClass {
if (isset($rq)) {
$body = $rq->getBody()->getContents();
} else {
throw new \Exception("Response is empty");
throw new EmptyResponseException();
}

$contents = json_decode($body);
Expand Down

0 comments on commit c4c82ab

Please sign in to comment.