Skip to content

Commit

Permalink
Adding exception handling
Browse files Browse the repository at this point in the history
Autoloading Guzzle client if it exists
  • Loading branch information
jaimz22 committed Jun 8, 2015
1 parent 1ab852f commit 41fb2dd
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Overcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'];
Expand All @@ -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;
}
}

/**
Expand Down

0 comments on commit 41fb2dd

Please sign in to comment.