Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 namespace Aws\Api\ErrorParser;
3
4 use Aws\Api\Parser\AbstractParser;
5 use Aws\Api\Parser\PayloadParserTrait;
6 use Aws\Api\StructureShape;
7 use Psr\Http\Message\ResponseInterface;
8
9 /**
10 * Provides basic JSON error parsing functionality.
11 */
12 trait JsonParserTrait
13 {
14 use PayloadParserTrait;
15
16 private function genericHandler(ResponseInterface $response): array
17 {
18 $code = (string) $response->getStatusCode();
19 $error_code = null;
20 $error_type = null;
21
22 // Parse error code and type for query compatible services
23 if ($this->api
24 && !is_null($this->api->getMetadata('awsQueryCompatible'))
25 && $response->hasHeader('x-amzn-query-error')
26 ) {
27 $awsQueryError = $this->parseAwsQueryCompatibleHeader($response);
28 if ($awsQueryError) {
29 $error_code = $awsQueryError['code'];
30 $error_type = $awsQueryError['type'];
31 }
32 }
33
34 // Parse error code from X-Amzn-Errortype header
35 if (!$error_code && $response->hasHeader('X-Amzn-Errortype')) {
36 $error_code = $this->extractErrorCode(
37 $response->getHeaderLine('X-Amzn-Errortype')
38 );
39 }
40
41 $parsedBody = null;
42
43 $rawBody = AbstractParser::getBodyContents($response);
44 if (!empty($rawBody)) {
45 $parsedBody = $this->parseJson($rawBody, $response);
46 }
47
48 // Parse error code from response body
49 if (!$error_code && $parsedBody) {
50 $error_code = $this->parseErrorFromBody($parsedBody);
51 }
52
53 if (!isset($error_type)) {
54 $error_type = $code[0] == '4' ? 'client' : 'server';
55 }
56
57 return [
58 'request_id' => $response->getHeaderLine('x-amzn-requestid'),
59 'code' => $error_code ?? null,
60 'message' => null,
61 'type' => $error_type,
62 'parsed' => $parsedBody
63 ];
64 }
65
66 /**
67 * Parse AWS Query Compatible error from header
68 *
69 * @param ResponseInterface $response
70 * @return array|null Returns ['code' => string, 'type' => string] or null
71 */
72 private function parseAwsQueryCompatibleHeader(ResponseInterface $response): ?array
73 {
74 $queryError = $response->getHeaderLine('x-amzn-query-error');
75 $parts = explode(';', $queryError);
76
77 if (count($parts) === 2 && $parts[0] && $parts[1]) {
78 return [
79 'code' => $parts[0],
80 'type' => $parts[1]
81 ];
82 }
83
84 return null;
85 }
86
87 /**
88 * Parse error code from response body
89 *
90 * @param array|null $parsedBody
91 * @return string|null
92 */
93 private function parseErrorFromBody(?array $parsedBody): ?string
94 {
95 if (!$parsedBody
96 || (!isset($parsedBody['code']) && !isset($parsedBody['__type']))
97 ) {
98 return null;
99 }
100
101 $error_code = $parsedBody['code'] ?? $parsedBody['__type'];
102 return $this->extractErrorCode($error_code);
103 }
104
105 /**
106 * Extract error code from raw error string containing # and/or : delimiters
107 *
108 * @param string $rawErrorCode
109 * @return string
110 */
111 private function extractErrorCode(string $rawErrorCode): string
112 {
113 // Handle format with both # and uri (e.g., "namespace#http://foo-bar")
114 if (str_contains($rawErrorCode, ':') && str_contains($rawErrorCode, '#')) {
115 $start = strpos($rawErrorCode, '#') + 1;
116 $end = strpos($rawErrorCode, ':', $start);
117 return substr($rawErrorCode, $start, $end - $start);
118 }
119
120 // Handle format with uri only : (e.g., "ErrorCode:http://foo-bar.com/baz")
121 if (str_contains($rawErrorCode, ':')) {
122 return substr($rawErrorCode, 0, strpos($rawErrorCode, ':'));
123 }
124
125 // Handle format with only # (e.g., "namespace#ErrorCode")
126 if (str_contains($rawErrorCode, '#')) {
127 return substr($rawErrorCode, strpos($rawErrorCode, '#') + 1);
128 }
129
130 return $rawErrorCode;
131 }
132
133 protected function payload(
134 ResponseInterface $response,
135 StructureShape $member
136 ) {
137 $rawBody = AbstractParser::getBodyContents($response);
138
139 if (!empty($rawBody)) {
140 $jsonBody = $this->parseJson($rawBody, $response);
141 } else {
142 $jsonBody = $rawBody;
143 }
144
145 return $this->parser->parse($member, $jsonBody);
146 }
147 }