| 1 |
efrain |
1 |
<?php
|
|
|
2 |
namespace Aws\Api\ErrorParser;
|
|
|
3 |
|
|
|
4 |
use Aws\Api\Parser\JsonParser;
|
|
|
5 |
use Aws\Api\Service;
|
|
|
6 |
use Aws\Api\StructureShape;
|
|
|
7 |
use Aws\CommandInterface;
|
|
|
8 |
use Psr\Http\Message\ResponseInterface;
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Parses JSON-REST errors.
|
|
|
12 |
*/
|
|
|
13 |
class RestJsonErrorParser extends AbstractErrorParser
|
|
|
14 |
{
|
|
|
15 |
use JsonParserTrait;
|
|
|
16 |
|
|
|
17 |
private $parser;
|
|
|
18 |
|
|
|
19 |
public function __construct(Service $api = null, JsonParser $parser = null)
|
|
|
20 |
{
|
|
|
21 |
parent::__construct($api);
|
|
|
22 |
$this->parser = $parser ?: new JsonParser();
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public function __invoke(
|
|
|
26 |
ResponseInterface $response,
|
|
|
27 |
CommandInterface $command = null
|
|
|
28 |
) {
|
|
|
29 |
$data = $this->genericHandler($response);
|
|
|
30 |
|
|
|
31 |
// Merge in error data from the JSON body
|
|
|
32 |
if ($json = $data['parsed']) {
|
|
|
33 |
$data = array_replace($data, $json);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Correct error type from services like Amazon Glacier
|
|
|
37 |
if (!empty($data['type'])) {
|
|
|
38 |
$data['type'] = strtolower($data['type']);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
// Retrieve the error code from services like Amazon Elastic Transcoder
|
|
|
42 |
if ($code = $response->getHeaderLine('x-amzn-errortype')) {
|
|
|
43 |
$colon = strpos($code, ':');
|
|
|
44 |
$data['code'] = $colon ? substr($code, 0, $colon) : $code;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
// Retrieve error message directly
|
|
|
48 |
$data['message'] = isset($data['parsed']['message'])
|
|
|
49 |
? $data['parsed']['message']
|
|
|
50 |
: (isset($data['parsed']['Message'])
|
|
|
51 |
? $data['parsed']['Message']
|
|
|
52 |
: null);
|
|
|
53 |
|
|
|
54 |
$this->populateShape($data, $response, $command);
|
|
|
55 |
|
|
|
56 |
return $data;
|
|
|
57 |
}
|
|
|
58 |
}
|