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