From 41fb2dd194c011b491f3e5b54b98bf6e1353cbe0 Mon Sep 17 00:00:00 2001 From: James Murray Date: Mon, 8 Jun 2015 15:54:08 -0400 Subject: [PATCH] Adding exception handling Autoloading Guzzle client if it exists --- src/Overcast.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Overcast.php b/src/Overcast.php index 5c93501..7fb66e0 100644 --- a/src/Overcast.php +++ b/src/Overcast.php @@ -59,7 +59,7 @@ public function __construct($apiKey, ClientAdapterInterface $adapter = null) { self::$apiKey = $apiKey; if (is_null($adapter)) { - if (class_exists('GuzzleHttp\Client',false)) { + if (class_exists('GuzzleHttp\Client',true)) { $adapter = new GuzzleClientAdapter(); }else{ $adapter = new FileGetContentsClientAdapter(); @@ -80,8 +80,9 @@ public function __construct($apiKey, ClientAdapterInterface $adapter = null) */ public function getForecast($latitude, $longitude, \DateTime $time = null) { - $response = $this->adapter->getForecast($latitude,$longitude, $time); - $responseHeaders = $this->adapter->getHeaders(); + try{ + $response = $this->adapter->getForecast($latitude, $longitude, $time); + $responseHeaders = $this->adapter->getHeaders(); if (!is_null($responseHeaders['apiCalls'])) { $this->apiCalls = $responseHeaders['apiCalls']; @@ -94,7 +95,10 @@ public function getForecast($latitude, $longitude, \DateTime $time = null) $cacheAge = (new \DateTime())->getTimestamp() - (new \DateTime($responseHeaders['cache']['expires']))->getTimestamp(); } - return new Forecast($response,$cacheAge, $responseHeaders['responseTime']); + return new Forecast($response, $cacheAge, $responseHeaders['responseTime']); + }catch(\Exception $e) { + return null; + } } /**