Skip to content

Commit

Permalink
Merge pull request #8 from printu/fix/getStatusCode
Browse files Browse the repository at this point in the history
fixed an Fatal Error when a timeout occured
  • Loading branch information
krzaczek committed Jan 13, 2016
2 parents 9a5bca1 + 5308ff8 commit d56b811
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Exception\ParseException;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Subscriber\Retry\RetrySubscriber;

/**
Expand Down Expand Up @@ -151,6 +152,14 @@ private function handleErrors()

// Stop other events from firing when you override 401 responses
$e->stopPropagation();

if (!$e->getResponse()) {
$response = new Response(502, [], null);
$response->setReasonPhrase($e->getException()->getMessage());
$e = CustomerioException::factory($e->getRequest(), $response, $e);
throw $e;
}

if ($e->getResponse()->getStatusCode() >= 400 && $e->getResponse()->getStatusCode() < 600) {
$e = CustomerioException::factory($e->getRequest(), $e->getResponse(), $e);
throw $e;
Expand Down
4 changes: 4 additions & 0 deletions src/Exception/CustomerioException.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public static function factory(RequestInterface $request, ResponseInterface $res
*/
private static function isValidError($responseBody)
{
if (!$responseBody || !is_array($responseBody)) {
return false;
}

if (array_key_exists('meta', $responseBody) &&
array_key_exists('error', $responseBody['meta'])
) {
Expand Down
33 changes: 33 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use Customerio\Exception\ServerErrorResponseException;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Subscriber\Mock;

/**
* Class ClientTest
Expand Down Expand Up @@ -120,4 +123,34 @@ public function testUnsuccessfulResponse()
$this->assertSame($e->getResponse()->getStatusCode(), 404);
}
}

/**
* @expectedException \Customerio\Exception\CustomerioException
*/
public function testTimeoutResponse()
{
$history = new History();

$config['api_key'] = 'key';
$config['site_id'] = 'site_id';

$client = new \Customerio\Client($config);
$httpClient = $client->getHttpClient();

$mock = new Mock();
$mock->addException(
new ConnectException(
'addCustomer',
new Request('post', '/')
)
);

$httpClient->getEmitter()->attach($mock);
$httpClient->getEmitter()->attach($history);

$client->addCustomer([
'id' => 45,
'email' => '[email protected]'
]);
}
}

0 comments on commit d56b811

Please sign in to comment.