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