Skip to content

Commit

Permalink
Keep all XML handling inside parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew committed Sep 9, 2015
1 parent 0814b6e commit 61f0eb3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
21 changes: 12 additions & 9 deletions src/S3/AmbiguousSuccessParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Aws\Exception\AwsException;
use Exception;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;

class AmbiguousSuccessParser extends AbstractParser
{
Expand All @@ -18,12 +17,18 @@ class AmbiguousSuccessParser extends AbstractParser

/** @var callable */
private $parser;
/** @var callable */
private $errorParser;
/** @var string */
private $exceptionClass;

public function __construct(callable $parser, $exceptionClass = AwsException::class)
{
public function __construct(
callable $parser,
callable $errorParser,
$exceptionClass = AwsException::class
) {
$this->parser = $parser;
$this->errorParser = $errorParser;
$this->exceptionClass = $exceptionClass;
}

Expand All @@ -34,12 +39,10 @@ public function __invoke(
if (200 === $response->getStatusCode()
&& isset(self::$ambiguousSuccesses[$command->getName()])
) {
$xml = new SimpleXMLElement($response->getBody());
if ('Error' === $xml->getName()) {
throw $this->parseError(
$xml->xpath('/Error/Message')[0],
$command
);
$errorParser = $this->errorParser;
$parsed = $errorParser($response);
if (isset($parsed['code']) && isset($parsed['message'])) {
throw $this->parseError($parsed['message'], $command);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/S3/S3Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,11 @@ public static function _applyApiProvider($value, array &$args, HandlerList $list
{
ClientResolver::_apply_api_provider($value, $args, $list);
$args['parser'] = new GetBucketLocationParser(
new AmbiguousSuccessParser($args['parser'], $args['exception_class'])
new AmbiguousSuccessParser(
$args['parser'],
$args['error_parser'],
$args['exception_class']
)
);
}

Expand Down
7 changes: 6 additions & 1 deletion tests/S3/S3ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function testRetries200Errors(
return new FulfilledPromise(new Response(
200,
[],
'<?xml version="1.0" encoding="UTF-8"?><node></node>'
$this->getWellFormedXml()
));
}

Expand Down Expand Up @@ -500,4 +500,9 @@ private function getMalformedXml()
<Size>10</Size>
EOXML;
}

private function getWellFormedXml()
{
return '<?xml version="1.0" encoding="UTF-8"?><node></node>';
}
}

0 comments on commit 61f0eb3

Please sign in to comment.