added more exception catch

This commit is contained in:
denisdulici 2019-12-19 18:02:51 +03:00
parent df8391158d
commit f551bac363

View File

@ -2,7 +2,9 @@
namespace App\Traits; namespace App\Traits;
use Exception;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
trait SiteApi trait SiteApi
@ -31,27 +33,29 @@ trait SiteApi
try { try {
$response = $client->request($method, $path, $options); $response = $client->request($method, $path, $options);
} catch (RequestException $e) { } catch (ConnectException | Exception | RequestException $e) {
$response = $e; $response = $e;
} }
return $response; return $response;
} }
public static function getResponse($method, $path, $data = []) public static function getResponse($method, $path, $data = [], $status_code = 200)
{ {
$response = static::siteApiRequest($method, $path, $data); $response = static::siteApiRequest($method, $path, $data);
if (!$response || ($response instanceof RequestException) || ($response->getStatusCode() != 200)) { $is_exception = (($response instanceof ConnectException) || ($response instanceof Exception) || ($response instanceof RequestException));
if (!$response || $is_exception || ($response->getStatusCode() != $status_code)) {
return false; return false;
} }
return $response; return $response;
} }
public static function getResponseData($method, $path, $data = []) public static function getResponseData($method, $path, $data = [], $status_code = 200)
{ {
if (!$response = static::getResponse($method, $path, $data)) { if (!$response = static::getResponse($method, $path, $data, $status_code)) {
return []; return [];
} }