AmbiguousSuccessParser.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\Api\Parser\AbstractParser;
  4. use Aws\Api\Parser\Exception\ParserException;
  5. use Aws\Api\StructureShape;
  6. use Aws\CommandInterface;
  7. use Aws\Exception\AwsException;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Psr\Http\Message\StreamInterface;
  10. /**
  11. * Converts errors returned with a status code of 200 to a retryable error type.
  12. *
  13. * @internal
  14. */
  15. class AmbiguousSuccessParser extends AbstractParser
  16. {
  17. private static $ambiguousSuccesses = [
  18. 'UploadPart' => true,
  19. 'UploadPartCopy' => true,
  20. 'CopyObject' => true,
  21. 'CompleteMultipartUpload' => true,
  22. ];
  23. /** @var callable */
  24. private $errorParser;
  25. /** @var string */
  26. private $exceptionClass;
  27. public function __construct(
  28. callable $parser,
  29. callable $errorParser,
  30. $exceptionClass = AwsException::class
  31. ) {
  32. $this->parser = $parser;
  33. $this->errorParser = $errorParser;
  34. $this->exceptionClass = $exceptionClass;
  35. }
  36. public function __invoke(
  37. CommandInterface $command,
  38. ResponseInterface $response
  39. ) {
  40. if (200 === $response->getStatusCode()
  41. && isset(self::$ambiguousSuccesses[$command->getName()])
  42. ) {
  43. $errorParser = $this->errorParser;
  44. try {
  45. $parsed = $errorParser($response);
  46. } catch (ParserException $e) {
  47. $parsed = [
  48. 'code' => 'ConnectionError',
  49. 'message' => "An error connecting to the service occurred"
  50. . " while performing the " . $command->getName()
  51. . " operation."
  52. ];
  53. }
  54. if (isset($parsed['code']) && isset($parsed['message'])) {
  55. throw new $this->exceptionClass(
  56. $parsed['message'],
  57. $command,
  58. ['connection_error' => true]
  59. );
  60. }
  61. }
  62. $fn = $this->parser;
  63. return $fn($command, $response);
  64. }
  65. public function parseMemberFromStream(
  66. StreamInterface $stream,
  67. StructureShape $member,
  68. $response
  69. ) {
  70. return $this->parser->parseMemberFromStream($stream, $member, $response);
  71. }
  72. }