1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace OpenSearch\Exception; |
6: | |
7: | |
8: | |
9: | |
10: | class ErrorMessageExtractor |
11: | { |
12: | public static function extractErrorMessage(string|array|null $data): ?string |
13: | { |
14: | if (is_string($data) || is_null($data)) { |
15: | return $data; |
16: | } |
17: | |
18: | |
19: | if (!isset($data['error'])) { |
20: | |
21: | |
22: | |
23: | return json_encode($data); |
24: | } |
25: | |
26: | |
27: | if (is_array($data['error']) && array_key_exists('reason', $data['error'])) { |
28: | |
29: | $info = $data['error']['root_cause'][0] ?? $data['error']; |
30: | $cause = $info['reason']; |
31: | $type = $info['type']; |
32: | |
33: | return "$type: $cause"; |
34: | } |
35: | |
36: | |
37: | $legacyError = $data['error']; |
38: | if (is_array($legacyError)) { |
39: | return json_encode($legacyError); |
40: | } |
41: | return $legacyError; |
42: | } |
43: | } |
44: | |