JsonRpcErrorParser.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. $parts = explode('#', $data['parsed']['__type']);
  30. $data['code'] = isset($parts[1]) ? $parts[1] : $parts[0];
  31. $data['message'] = isset($data['parsed']['message'])
  32. ? $data['parsed']['message']
  33. : null;
  34. }
  35. $this->populateShape($data, $response, $command);
  36. return $data;
  37. }
  38. }