| 1 |
<?php |
| 2 |
namespace Aws\Api\ErrorParser; |
| 3 |
|
| 4 |
use Aws\Api\Parser\AbstractParser; |
| 5 |
use Aws\Api\StructureShape; |
| 6 |
use Aws\CommandInterface; |
| 7 |
use Psr\Http\Message\ResponseInterface; |
| 8 |
use Psr\Http\Message\StreamInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
abstract class AbstractRpcV2ErrorParser extends AbstractErrorParser |
| 16 |
{ |
| 17 |
private const HEADER_QUERY_ERROR = 'x-amzn-query-error'; |
| 18 |
private const HEADER_ERROR_TYPE = 'x-amzn-errortype'; |
| 19 |
private const HEADER_REQUEST_ID = 'x-amzn-requestid'; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __invoke( |
| 28 |
ResponseInterface $response, |
| 29 |
?CommandInterface $command = null |
| 30 |
) { |
| 31 |
$response = AbstractParser::getResponseWithCachingStream($response); |
| 32 |
$data = $this->parseError($response); |
| 33 |
|
| 34 |
if (isset($data['parsed']['__type'])) { |
| 35 |
$data['message'] = $data['parsed']['message'] ?? null; |
| 36 |
} |
| 37 |
|
| 38 |
$this->populateShape($data, $response, $command); |
| 39 |
|
| 40 |
return $data; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
abstract protected function payload( |
| 50 |
ResponseInterface $response, |
| 51 |
StructureShape $member |
| 52 |
): array; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
abstract protected function parseBody( |
| 61 |
StreamInterface $body, |
| 62 |
ResponseInterface $response |
| 63 |
): mixed; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private function parseError(ResponseInterface $response): array |
| 71 |
{ |
| 72 |
$statusCode = (string) $response->getStatusCode(); |
| 73 |
$errorCode = null; |
| 74 |
$errorType = null; |
| 75 |
|
| 76 |
if ($this->api?->getMetadata('awsQueryCompatible') !== null |
| 77 |
&& $response->hasHeader(self::HEADER_QUERY_ERROR) |
| 78 |
&& $awsQueryError = $this->parseQueryCompatibleHeader($response) |
| 79 |
) { |
| 80 |
$errorCode = $awsQueryError['code']; |
| 81 |
$errorType = $awsQueryError['type']; |
| 82 |
} |
| 83 |
|
| 84 |
if (!$errorCode && $response->hasHeader(self::HEADER_ERROR_TYPE)) { |
| 85 |
$errorCode = $this->extractErrorCode( |
| 86 |
$response->getHeaderLine(self::HEADER_ERROR_TYPE) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
$parsedBody = null; |
| 91 |
$body = $response->getBody(); |
| 92 |
if ($body->getSize()) { |
| 93 |
|
| 94 |
$parsedBody = array_change_key_case($this->parseBody($body, $response)); |
| 95 |
} |
| 96 |
|
| 97 |
if (!$errorCode && $parsedBody) { |
| 98 |
$errorCode = $this->extractErrorCode( |
| 99 |
$parsedBody['code'] ?? $parsedBody['__type'] ?? '' |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
return [ |
| 104 |
'request_id' => $response->getHeaderLine(self::HEADER_REQUEST_ID), |
| 105 |
'code' => $errorCode ?: null, |
| 106 |
'message' => null, |
| 107 |
'type' => $errorType ?? ($statusCode[0] === '4' ? 'client' : 'server'), |
| 108 |
'parsed' => $parsedBody, |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private function parseQueryCompatibleHeader(ResponseInterface $response): ?array |
| 120 |
{ |
| 121 |
$parts = explode(';', $response->getHeaderLine(self::HEADER_QUERY_ERROR)); |
| 122 |
if (count($parts) === 2 && $parts[0] && $parts[1]) { |
| 123 |
return [ |
| 124 |
'code' => $parts[0], |
| 125 |
'type' => $parts[1], |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
return null; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private function extractErrorCode(string $rawErrorCode): string |
| 139 |
{ |
| 140 |
|
| 141 |
if (str_contains($rawErrorCode, ':') && str_contains($rawErrorCode, '#')) { |
| 142 |
$start = strpos($rawErrorCode, '#') + 1; |
| 143 |
$end = strpos($rawErrorCode, ':', $start); |
| 144 |
return substr($rawErrorCode, $start, $end - $start); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
if (str_contains($rawErrorCode, ':')) { |
| 149 |
return substr($rawErrorCode, 0, strpos($rawErrorCode, ':')); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if (str_contains($rawErrorCode, '#')) { |
| 154 |
return substr($rawErrorCode, strpos($rawErrorCode, '#') + 1); |
| 155 |
} |
| 156 |
|
| 157 |
return $rawErrorCode; |
| 158 |
} |
| 159 |
} |