From 0494e0651a764fce2ee6d821c77da20fbf977495 Mon Sep 17 00:00:00 2001 From: Jonathan Eskew Date: Wed, 9 Sep 2015 23:08:15 -0700 Subject: [PATCH] Throw a ParserException when content is unparseable --- src/Api/Parser/Exception/ParserException.php | 4 ++ src/Api/Parser/JsonRpcParser.php | 4 +- src/Api/Parser/PayloadParserTrait.php | 51 ++++++++++++++++++++ src/Api/Parser/QueryParser.php | 4 +- src/Api/Parser/RestJsonParser.php | 4 +- src/Api/Parser/RestXmlParser.php | 4 +- src/S3/MalformedResponseParser.php | 6 +-- tests/DynamoDb/DynamoDbClientTest.php | 4 +- 8 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 src/Api/Parser/Exception/ParserException.php create mode 100644 src/Api/Parser/PayloadParserTrait.php diff --git a/src/Api/Parser/Exception/ParserException.php b/src/Api/Parser/Exception/ParserException.php new file mode 100644 index 0000000000..5eefb74f5a --- /dev/null +++ b/src/Api/Parser/Exception/ParserException.php @@ -0,0 +1,4 @@ +parser->parse( $operation->getOutput(), - json_decode($response->getBody(), true) + $this->parseJson($response->getBody()) )); } } diff --git a/src/Api/Parser/PayloadParserTrait.php b/src/Api/Parser/PayloadParserTrait.php new file mode 100644 index 0000000000..1a8542aa5d --- /dev/null +++ b/src/Api/Parser/PayloadParserTrait.php @@ -0,0 +1,51 @@ +message); + } + } catch (\Exception $e) { + throw new ParserException("Error parsing XML: {$e->getMessage()}", 0, $e); + } finally { + libxml_use_internal_errors($priorSetting); + } + + return $xmlPayload; + } +} diff --git a/src/Api/Parser/QueryParser.php b/src/Api/Parser/QueryParser.php index bf0135987b..4de6c675fe 100644 --- a/src/Api/Parser/QueryParser.php +++ b/src/Api/Parser/QueryParser.php @@ -11,6 +11,8 @@ */ class QueryParser extends AbstractParser { + use PayloadParserTrait; + /** @var XmlParser */ private $xmlParser; @@ -39,7 +41,7 @@ public function __invoke( ResponseInterface $response ) { $output = $this->api->getOperation($command->getName())->getOutput(); - $xml = new \SimpleXMLElement($response->getBody()); + $xml = $this->parseXml($response->getBody()); if ($this->honorResultWrapper && $output['resultWrapper']) { $xml = $xml->{$output['resultWrapper']}; diff --git a/src/Api/Parser/RestJsonParser.php b/src/Api/Parser/RestJsonParser.php index c346a6125a..274c4804a6 100644 --- a/src/Api/Parser/RestJsonParser.php +++ b/src/Api/Parser/RestJsonParser.php @@ -10,6 +10,8 @@ */ class RestJsonParser extends AbstractRestParser { + use PayloadParserTrait; + /** @var JsonParser */ private $parser; @@ -28,7 +30,7 @@ protected function payload( StructureShape $member, array &$result ) { - $jsonBody = json_decode($response->getBody(), true); + $jsonBody = $this->parseJson($response->getBody()); if ($jsonBody) { $result += $this->parser->parse($member, $jsonBody); diff --git a/src/Api/Parser/RestXmlParser.php b/src/Api/Parser/RestXmlParser.php index 8088d09856..1d02831462 100644 --- a/src/Api/Parser/RestXmlParser.php +++ b/src/Api/Parser/RestXmlParser.php @@ -10,6 +10,8 @@ */ class RestXmlParser extends AbstractRestParser { + use PayloadParserTrait; + /** @var XmlParser */ private $parser; @@ -28,7 +30,7 @@ protected function payload( StructureShape $member, array &$result ) { - $xml = new \SimpleXMLElement($response->getBody()); + $xml = $this->parseXml($response->getBody()); $result += $this->parser->parse($member, $xml); } } diff --git a/src/S3/MalformedResponseParser.php b/src/S3/MalformedResponseParser.php index de8effb294..6ab78e1a7e 100644 --- a/src/S3/MalformedResponseParser.php +++ b/src/S3/MalformedResponseParser.php @@ -2,9 +2,9 @@ namespace Aws\S3; use Aws\Api\Parser\AbstractParser; +use Aws\Api\Parser\Exception\ParserException; use Aws\CommandInterface; use Aws\Exception\AwsException; -use Exception; use Psr\Http\Message\ResponseInterface; class MalformedResponseParser extends AbstractParser @@ -30,10 +30,10 @@ public function __invoke( try { return $fn($command, $response); - } catch (Exception $e) { + } catch (ParserException $e) { throw new $this->exceptionClass( "Error parsing response for {$command->getName()}:" - . " AWS parsing error: {$e->getMessage()}", + . " AWS parsing error: {$e->getMessage()}", $command, ['connection_error' => true, 'exception' => $e], $e diff --git a/tests/DynamoDb/DynamoDbClientTest.php b/tests/DynamoDb/DynamoDbClientTest.php index 03d9b0c4ea..952484bcfb 100644 --- a/tests/DynamoDb/DynamoDbClientTest.php +++ b/tests/DynamoDb/DynamoDbClientTest.php @@ -79,8 +79,8 @@ public function dataForFormatValueTest() public function testValidatesAndRetriesCrc32() { $queue = [ - new Response(200, ['x-amz-crc32' => '123'], 'foo'), - new Response(200, ['x-amz-crc32' => '2356372769'], 'foo') + new Response(200, ['x-amz-crc32' => '123'], '"foo"'), + new Response(200, ['x-amz-crc32' => '400595255'], '"foo"') ]; $handler = function ($request, $options) use (&$queue) {