Skip to content

Commit

Permalink
Test and fix handling of internal server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 26, 2015
1 parent 40b4cdc commit 9c80e98
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/V2/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace GW2Treasures\GW2Api\V2;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Message\ResponseInterface;
Expand Down Expand Up @@ -59,7 +59,7 @@ protected function request( array $query = [], $url = null, $method = 'GET', $op

try {
$response = $this->getClient()->send( $request );
} catch( ClientException $ex ) {
} catch( BadResponseException $ex ) {
if( $ex->hasResponse() ) {
$response = $ex->getResponse();

Expand Down Expand Up @@ -106,10 +106,10 @@ protected function requestMany( array $queries = [], $url = null, $method = 'GET
$results = Pool::batch( $this->getClient(), $requests, [ 'pool_size' => 128 ]);

foreach( $results as $response ) {
/** @var Response|ClientException|\Exception $response */
/** @var Response|BadResponseException|\Exception $response */

if( $response instanceof \Exception ) {
if( $response instanceof ClientException && $response->hasResponse() ) {
if( $response instanceof BadResponseException && $response->hasResponse() ) {
$response = $response->getResponse();

foreach( $this->handlers as $handler ) {
Expand Down
12 changes: 12 additions & 0 deletions tests/ApiExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ public function testResponse() {
$this->assertNotFalse( strstr( $exception->__toString(), 'status: 400' ));
}
}

/**
* @expectedException \GW2Treasures\GW2Api\Exception\ApiException
* @expectedExceptionMessage Unknown GW2Api error
*/
public function testUnknownException() {
$this->mockResponse( new Response(
500, [], Stream::factory( 'Internal server error' )
));

$this->getEndpoint()->test();
}
}

0 comments on commit 9c80e98

Please sign in to comment.