Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/ErrorParser/AbstractRpcV2ErrorParser.php

raw symbols SCIP
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 * Base implementation for Smithy RPC V2 protocol error parsers.
12 *
13 * @internal
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 * @param ResponseInterface $response
23 * @param CommandInterface|null $command
24 *
25 * @return array
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 * @param ResponseInterface $response
45 * @param StructureShape $member
46 *
47 * @return array
48 */
49 abstract protected function payload(
50 ResponseInterface $response,
51 StructureShape $member
52 ): array;
53
54 /**
55 * @param StreamInterface $body
56 * @param ResponseInterface $response
57 *
58 * @return mixed
59 */
60 abstract protected function parseBody(
61 StreamInterface $body,
62 ResponseInterface $response
63 ): mixed;
64
65 /**
66 * @param ResponseInterface $response
67 *
68 * @return array
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 //TODO handle unseekable streams with CachingStream
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 * Parse AWS Query Compatible error from header
114 *
115 * @param ResponseInterface $response
116 *
117 * @return array|null Returns ['code' => string, 'type' => string] or null
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 * Extract error code from raw error string containing # and/or : delimiters
134 *
135 * @param string $rawErrorCode
136 * @return string
137 */
138 private function extractErrorCode(string $rawErrorCode): string
139 {
140 // Handle format with both # and uri (e.g., "namespace#ErrorCode:http://foo-bar")
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 // Handle format with uri only : (e.g., "ErrorCode:http://foo-bar.com/baz")
148 if (str_contains($rawErrorCode, ':')) {
149 return substr($rawErrorCode, 0, strpos($rawErrorCode, ':'));
150 }
151
152 // Handle format with only # (e.g., "namespace#ErrorCode")
153 if (str_contains($rawErrorCode, '#')) {
154 return substr($rawErrorCode, strpos($rawErrorCode, '#') + 1);
155 }
156
157 return $rawErrorCode;
158 }
159 }