Skip to content

Commit

Permalink
Merge pull request #735 from bshaffer/adds-json-error-handling
Browse files Browse the repository at this point in the history
ensures json errors are not thrown
  • Loading branch information
bshaffer committed Nov 6, 2015
2 parents e585962 + da06e03 commit 02d6c48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Google/Http/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ParseException;

/**
* This class implements the RESTful transport of apiServiceRequest()'s
Expand Down Expand Up @@ -93,10 +94,21 @@ public static function decodeHttpResponse(
) {
$body = (string) $response->getBody();
$code = $response->getStatusCode();
$result = null;

// return raw response when "alt" is "media"
$isJson = !($request && 'media' == $request->getQuery()->get('alt'));
$result = $isJson ? $response->json() : $body;

// set the result to the body if it's not set to anything else
if ($isJson) {
try {
$result = $response->json();
} catch (ParseException $e) {
$result = $body;
}
} else {
$result = $body;
}

// retry strategy
if ((intVal($code)) >= 300) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Google/Http/RESTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ public function tesProperErrorFormatting()
$response = new Response(401, array(), $stream);
$this->rest->decodeHttpResponse($response);
}

/**
* @expectedException Google_Service_Exception
*/
public function testNotJson404Error()
{
$stream = Stream::factory('Not Found');
$response = new Response(404, array(), $stream);
$this->rest->decodeHttpResponse($response);
}
}

0 comments on commit 02d6c48

Please sign in to comment.