JsonRpcErrorParser.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Aws\Api\ErrorParser;
  3. use Aws\Api\Parser\JsonParser;
  4. use Aws\Api\Service;
  5. use Aws\CommandInterface;
  6. use Psr\Http\Message\ResponseInterface;
  7. /**
  8. * Parsers JSON-RPC errors.
  9. */
  10. class JsonRpcErrorParser extends AbstractErrorParser
  11. {
  12. use JsonParserTrait;
  13. private $parser;
  14. public function __construct(Service $api = null, JsonParser $parser = null)
  15. {
  16. parent::__construct($api);
  17. $this->parser = $parser ?: new JsonParser();
  18. }
  19. public function __invoke(
  20. ResponseInterface $response,
  21. CommandInterface $command = null
  22. ) {
  23. $data = $this->genericHandler($response);
  24. // Make the casing consistent across services.
  25. if ($data['parsed']) {
  26. $data['parsed'] = array_change_key_case($data['parsed']);
  27. }
  28. if (isset($data['parsed']['__type'])) {
  29. if (!isset($data['code'])) {
  30. $parts = explode('#', $data['parsed']['__type']);
  31. $data['code'] = isset($parts[1]) ? $parts[1] : $parts[0];
  32. }
  33. $data['message'] = isset($data['parsed']['message'])
  34. ? $data['parsed']['message']
  35. : null;
  36. }
  37. $this->populateShape($data, $response, $command);
  38. return $data;
  39. }
  40. }