|
5 | 5 | use AsyncAws\Core\Exception\RuntimeException; |
6 | 6 | use AsyncAws\Core\Exception\UnexpectedValue; |
7 | 7 | use AsyncAws\Core\Exception\UnparsableResponse; |
8 | | -use Symfony\Contracts\HttpClient\ResponseInterface; |
9 | 8 |
|
10 | 9 | /** |
11 | 10 | * @internal |
12 | 11 | */ |
13 | | -class AwsErrorFactory |
| 12 | +class XmlAwsErrorFactory implements AwsErrorFactoryInterface |
14 | 13 | { |
15 | | - public static function createFromResponse(ResponseInterface $response): AwsError |
16 | | - { |
17 | | - $content = $response->getContent(false); |
18 | | - $headers = $response->getHeaders(false); |
19 | | - |
20 | | - return self::createFromContent($content, $headers); |
21 | | - } |
| 14 | + use AwsErrorFactoryFromResponseTrait; |
22 | 15 |
|
23 | | - public static function createFromContent(string $content, array $headers): AwsError |
| 16 | + public function createFromContent(string $content, array $headers): AwsError |
24 | 17 | { |
25 | 18 | try { |
26 | | - // Try json_decode it first, fallback to XML |
27 | | - if ($body = json_decode($content, true)) { |
28 | | - return self::parseJson($body, $headers); |
29 | | - } |
30 | | - |
31 | | - /** @phpstan-ignore-next-line */ |
| 19 | + /** |
| 20 | + * @phpstan-ignore-next-line |
| 21 | + * @psalm-suppress InvalidArgument |
| 22 | + */ |
32 | 23 | set_error_handler(static function ($errno, $errstr) { |
33 | 24 | throw new RuntimeException($errstr, $errno); |
34 | 25 | }); |
@@ -67,27 +58,4 @@ private static function parseXml(\SimpleXMLElement $xml): AwsError |
67 | 58 |
|
68 | 59 | throw new UnexpectedValue('XML does not contains AWS Error'); |
69 | 60 | } |
70 | | - |
71 | | - private static function parseJson(array $body, array $headers): AwsError |
72 | | - { |
73 | | - $code = null; |
74 | | - |
75 | | - $message = $body['message'] ?? $body['Message'] ?? null; |
76 | | - if (isset($headers['x-amzn-errortype'][0])) { |
77 | | - $code = explode(':', $headers['x-amzn-errortype'][0], 2)[0]; |
78 | | - } |
79 | | - |
80 | | - $type = $body['type'] ?? $body['Type'] ?? null; |
81 | | - if (isset($body['__type'])) { |
82 | | - $parts = explode('#', $body['__type'], 2); |
83 | | - $code = $parts[1] ?? $parts[0]; |
84 | | - $type = $parts[0]; |
85 | | - } |
86 | | - |
87 | | - if (null !== $code || null !== $message) { |
88 | | - return new AwsError($code, $message, $type, null); |
89 | | - } |
90 | | - |
91 | | - throw new UnexpectedValue('JSON does not contains AWS Error'); |
92 | | - } |
93 | 61 | } |
0 commit comments